# -*- coding: utf-8 -*-
# $Id: utils.py 1634 2013-04-12 15:36:36Z amelung $
#
# Copyright (c) 2007-2011 Otto-von-Guericke-Universität Magdeburg
#
# This file is part of ECSpooler.
"""
Some helper methods
"""
import os
import time
import random
import socket
import tempfile
import errno
try:
import hashlib
except ImportError:
"""
Generates a unique identifier. The prefix can be set or left blank.
@param: prefix The identifier's prefix (e.g. Student for student's Haskell module).
@return: A unique identifier with or without a prefix
"""
"""
Generates a absolute path string including the path to main temp dir,
the name of the module and a suffix.
@param: name The content of this file.
@param: suffix The file's suffix (e.g. extension). Default is ''
@return: A string with absolute file path
"""
"""
Writes a file with the given *absolute* filepath and content.
@param: content The content of this file.
@param: filename The absolut e file path.
@return: nothing
"""
#f = open(filename, 'w+')
f.write('\n')
f.flush()
f.close()
"""
Removes entire directory trees. Be careful with it, since it deletes
a lot of stuff. It is a recursive function.
@param: path absolute path
@deprecated
"""
try:
# it's a directory reucursive call to function again
else:
# it's a file, delete it
# delete the directory here
return True
except Exception:
#LOG.error(traceback.format_exc())
return False
"""
Generates a universally unique Id.
Any arguments only create more randomness.
"""
try:
except:
# if we can't get a network address, just imagine one
#data = md5.md5(data).hexdigest()
return data
"""Returns a list with no duplicate items"""
if idFunction is None:
def idFunction(x): return x
seen = {}
result = []
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
return result
"""
Write the PID in the named PID file.
Get the numeric process ID (“PID”) of the current process
and write it to the named file as a line of text.
@param pid_filename: Absolute path to PID file
"""
open_mode = 0644
#
# The file must consist of the process identifier in
# ASCII-encoded decimal, followed by a newline character. For
# would contain three characters: two, five, and newline.
"""
Read the PID recorded in the named PID file.
Read and return the numeric PID recorded as text in the named
PID file. If the PID file cannot be read, or if the content is
not a valid PID, return ``None``.
@param pid_filename: Absolute path to PID file
@return: PID or None
"""
pid = None
try:
except IOError:
pass
else:
#
# The file must consist of the process identifier in
# ASCII-encoded decimal, followed by a newline character.
#
# Programs that read PID files should be somewhat flexible
# in what they accept; i.e., they should ignore extra
# whitespace, leading zeroes, absence of the trailing
# newline, or additional lines in the PID file.
try:
except ValueError:
pass
return pid
"""
Remove the named PID file if it exists.
Removing a PID file that doesn't already exist puts us in the
desired state, so we ignore the condition if the file does not
exist.
@param pid_filename: Absolute path to PID file
"""
try:
pass
else:
raise