def main(): """ Controller. """ args = sys.argv[1:] if len(args) == 0: print_usage() sys.exit(0) args = check_args(args) prg = args[0] # script in venv args = args[1:] # arguments of the script in venv p = Path(prg).absolute() venv_file = find_venv_file(p.parent) venv_dir = Path(venv_file.read_file().strip()) # .venv can also contain a relative path if not venv_dir.isabsolute(): venv_dir = Path(venv_file.parent, venv_dir).norm() if not venv_dir.isdir(): print("Error: {vd} is not a directory.".format(vd=venv_dir), file=sys.stderr) sys.exit(1) # python_path = Path(venv_dir, "bin/python") if not python_path.isfile(): print("Error: {pp} is missing.".format(pp=python_path), file=sys.stderr) sys.exit(1) if DEBUG: print("# venv dir: {d}".format(d=venv_dir), file=sys.stderr) my_call(python_path, prg, args)
def content(request=None): base = Path(current_app.config.get('INUPYPI_REPO', Path('.', 'packages'))) if request: repo = Path(base, request) else: repo = base try: repo = repo.absolute() base = base.absolute() if not repo.exists(): if base == repo: raise InuPyPIMissingRepoPath # sets the request to lowercase and compares it with # the existing items in the repository in lowercase repo = search_path(repo, base) if not repo: raise InuPyPI404Exception if repo.isdir(): return Dirs(repo) if repo.isfile(): return repo except InuPyPIMissingRepoPath: abort(500, 'Missing repository or package path!') except InuPyPI404Exception: abort(404, 'Path or File could not be found!') except: abort(500, 'Internal Server Error!') return repo
def find_venv_file(folder): """ Find the .venv file. If necessary, go up to the parent folders. """ venv_file = Path(folder, VENV_FILE) while not (venv_file.isfile() or folder == "/"): folder = folder.parent venv_file = Path(folder, VENV_FILE) # if venv_file.isfile(): if DEBUG: print("# venv file: {f}".format(f=venv_file), file=sys.stderr) return venv_file # else print("Error: {f} file is missing.".format(f=VENV_FILE), file=sys.stderr) sys.exit(1)
def append(self, key): key_path = Path(str(key)) if key_path.isfile(): with open(str(key_path)) as f: key = f.read() if not isinstance(key, bytes): key = key.encode('utf-8') if key in self: return directory = Path(self.user.path, 'keydir', self.user.name, hashlib.md5(key.strip().split()[1]).hexdigest()) directory.mkdir(parents=True) key_file = Path(directory, "%s.pub" % self.user.name) if key_file.exists() and key_file.read_file() == key: return key_file.write_file(key, mode='wb') self.user.git.commit(['keydir'], 'Added new key for user %s' % self.user.name) super(ListKeys, self).append(key)
def find_file_recursive(filename): """Search for a file recursively up""" current_dir = Path(__file__).absolute().parent while current_dir and current_dir != current_dir.parent: fname = Path(current_dir, filename) if fname.isfile(): return fname current_dir = current_dir.parent return False
class ReleaseUnpackerRarFile(object): """Release unpacker RAR file.""" def __init__(self, rar_file_path): """Initialize and validate rar file path.""" self.rar_file_path = Path(rar_file_path) if (not self.rar_file_path.exists() or not self.rar_file_path.isfile() or not self.rar_file_path.ext == ".rar"): raise ReleaseUnpackerRarFileError("Invalid RAR file {}".format( self.rar_file_path)) self.rar_file_path_abs = self.rar_file_path.absolute() self.rar_file = rarfile.RarFile(self.rar_file_path) def __repr__(self): """Return object string representation.""" return "<ReleaseUnpackerRarFile: {}>".format(self.rar_file_path_abs) @lazy def name(self): """Return name of release folder.""" if self.subs_dir: name = self.rar_file_path.parent.parent.name else: name = self.rar_file_path.parent.name return str(name) @lazy def subs_dir(self): """Return True if RAR file is located in a Subs folder.""" if self.rar_file_path.parent.name.lower() in ("subs", "sub"): return True else: return False @lazy def file_list(self): """Return file list of RAR file.""" files = [] for file in self.rar_file.infolist(): files.append({"name": Path(file.filename), "size": file.file_size}) return files def extract_file(self, file_name, unpack_dir): """Extract file_name and return extracted file path.""" self.rar_file.extract(file_name, path=unpack_dir) self.extracted_file_path = Path(unpack_dir, file_name) # Set the mtime to current time self.set_mtime() return self.extracted_file_path def set_mtime(self): """Set mtime of extracted file path to current time.""" os.utime(self.extracted_file_path, None)
def find_json(dname): p = Path(dname, "firefox") with ChDir(p): profile = glob("*.default")[0] p = Path(p, profile) p = Path(p, "jetpack", "jid1-xUfzOsOFlzSOXg@jetpack") p = Path(p, "simple-storage", "store.json") assert p.isfile() return p
def process(appname): appdir = Path(appname) if not appdir.isdir(): print("Error: there is no app called {0}.".format(appdir)) sys.exit(1) # else static = Path(appname, 'static', appname) static.mkdir(True) templates = Path(appname, 'templates', appname) templates.mkdir(True) urls = Path(appname, 'urls.py') if not urls.isfile(): urls.write_file(urls_py)
class ReleaseUnpackerRarFile(object): def __init__(self, rar_file_path): self.rar_file_path = Path(rar_file_path) if (not self.rar_file_path.exists() or not self.rar_file_path.isfile() or not self.rar_file_path.ext == '.rar'): raise ReleaseUnpackerRarFileError('Invalid RAR file {}'.format( self. rar_file_path)) self.rar_file_path_abs = self.rar_file_path.absolute() self.rar_file = rarfile.RarFile(self.rar_file_path) def __repr__(self): return '<ReleaseUnpackerRarFile: {}>'.format(self.rar_file_path_abs) @lazy def name(self): if self.subs_dir: name = self.rar_file_path.parent.parent.name else: name = self.rar_file_path.parent.name return str(name) @lazy def subs_dir(self): if self.rar_file_path.parent.name.lower() in ('subs', 'sub'): return True else: return False @lazy def file_list(self): files = [] for file in self.rar_file.infolist(): files.append({'name': Path(file.filename), 'size': file.file_size}) return files def set_mtime(self): with file(self.extracted_file_path, 'a'): os.utime(self.extracted_file_path, None) def extract_file(self, file_name, unpack_dir): self.rar_file.extract(file_name, path=unpack_dir) self.extracted_file_path = Path(unpack_dir, file_name) # Set the mtime to current time self.set_mtime() return self.extracted_file_path
def deploy(request, project_id): """ Deploy project to specific location """ try: project = Project.objects.get(pk=project_id) except: return HttpResponse("Project not found"); path = Path(project.path) if path.isfile(): return HttpResponse("Expected deploy path to be a directory"); # add tasks to messagequeue server tasks.deploy.delay(model_to_dict(project)) return HttpResponse("Task dispatched, your project should be deployed shortly.");
def append(self, key): key_path = Path(key) if key_path.isfile(): with open(str(key_path)) as f: key = f.read() if key in self: return directory = Path(self.user.path, 'keydir', self.user.name, hashlib.md5(key.strip().split()[1]).hexdigest()) directory.mkdir(parents=True) key_file = Path(directory, "%s.pub" % self.user.name) if key_file.exists() and key_file.read_file() == key: return key_file.write_file(key) self.user.git.commit(['keydir'], 'Added new key for user %s' % self.user.name) super(ListKeys, self).append(key)
print(Path("$HOME").expand_vars()) # Expands system variables print(Path("/home/luke/..").norm()) # Expands .. and . notation print(Path("$HOME/..").expand()) # Expands system variables, ~ and also .. # Expands system variable and ~. Will also normalise the path ( remove redundant # .. . incorrect slashes and correct capitalisation on case sensitive file systems. Calls os.path.normpath # File Attributes and permissions print("\n*** File Attributes and permissions") # noinspection PyArgumentList print(here.atime()) # Last access time; seconds past epcoh # noinspection PyArgumentList print(here.ctime()) # Last permission or ownership modification; windows is creation time; # noinspection PyArgumentList print(here.isfile()) # Is a file; symbolic links are followed. print(here.isdir()) # Is a directory; symbolic links are followed. # noinspection PyArgumentList print(here.islink()) # Is a symbolic link # noinspection PyArgumentList print(here.ismount()) # Is a mount point; ie the parent is on a different device. # noinspection PyArgumentList print(here.exists()) # File exists; symbolic links are followed. # noinspection PyArgumentList print(here.lexists()) # Same as exists but symbolic links are not followed. # noinspection PyArgumentList print(here.size()) # File size in bytes. print(Path("/foo").isabsolute()) # Is absolute and not relative path # Epoch? print("\n*** gmtime")
print(Path("$HOME").expand_vars()) # Expands system variables print(Path("/home/luke/..").norm()) # Expands .. and . notation print(Path("$HOME/..").expand()) # Expands system variables, ~ and also .. # Expands system variable and ~. Will also normalise the path ( remove redundant # .. . incorrect slashes and correct capitalisation on case sensitive file systems. Calls os.path.normpath # File Attributes and permissions print("\n*** File Attributes and permissions") # noinspection PyArgumentList print(here.atime()) # Last access time; seconds past epcoh # noinspection PyArgumentList print(here.ctime() ) # Last permission or ownership modification; windows is creation time; # noinspection PyArgumentList print(here.isfile()) # Is a file; symbolic links are followed. print(here.isdir()) # Is a directory; symbolic links are followed. # noinspection PyArgumentList print(here.islink()) # Is a symbolic link # noinspection PyArgumentList print(here.ismount() ) # Is a mount point; ie the parent is on a different device. # noinspection PyArgumentList print(here.exists()) # File exists; symbolic links are followed. # noinspection PyArgumentList print(here.lexists()) # Same as exists but symbolic links are not followed. # noinspection PyArgumentList print(here.size()) # File size in bytes. print(Path("/foo").isabsolute()) # Is absolute and not relative path # Epoch?
def is_a_command(self, request): """ is the request a command ? trying to recognize a command and keep command and args to avoid double parsing """ request = request.lower() if request.endswith('?'): request = request[:-1] is_command = False # @todo meet a problem when command has two or more words, # need to check this also command_list = request.split() path = Path( ROBOT_DIR.child('core', 'brain') + '/' + '/'.join(request.split()) + '/reaction.py') if not is_command and len(command_list) > 1: logger.info('Command %s consist of %d words' % ( request, len(command_list))) self._cmd_args = command_list self._cmd_path = [] # this method will try to find first full cmd # after that one level up if path.isfile(): is_command = True logger.info('Reaction file has been found at %s', self._cmd_path) logger.info('Request is command .. continue') # set something like ['ping', 'my', 'sites'] self._cmd_path = self._cmd_args # last added # user defined script if path.isfile() and not is_command: logger.info('User defined reaction found at %s', path) is_command = True # sets something like ['ping', 'my', 'sites'] #self._cmd_path = path.relative().parent.split('/') self._cmd_path = command_list self._cmd_args = command_list logger.info('cmd_path %s', self._cmd_path) # # First check for simple(one word) commands # why ? # moved to second position # example : I want to know : who is looking for a thing in internet # instead of who is Chuck Norris if is_command is False: logger.info('Check for embedded..') if request.startswith(settings.EMBEDDED_COMMANDS): logger.info('Embedded command detected %s!' % self._cmd_path) for c in settings.EMBEDDED_COMMANDS: if request.startswith(c): break self._cmd_path = c.split() self._cmd_args = request.replace(c, '').split() is_command = True if is_command is False: self._request_processed = False self._continue_dialog = True logger.info('Request is not a command .. continue') return is_command
def get_file(eggbasket, package, filename): package_file = Path(get_package_path(eggbasket), package, filename) if package_file.exists() and package_file.isfile(): return package_file return False