def main(): """ main """ cwd = os.getcwd() arguments = IArguments(__doc__) for fp in os.listdir(arguments.filepath): if os.path.isdir(fp): fp = os.path.join(arguments.filepath, fp) fpm = os.path.join(fp, 'merge.sh') if os.path.exists(fpm): os.chdir(fp) console(fp) os.system("git pull; git commit -am '-'; git push") code, res = cmd_exec(fpm) if code == 128: console(fp) print("rm -Rf", fp) return if code != 0: os.chdir(fp) return else: console("merge.sh path does not exist") console(fp) print("rm -Rf", fp) return os.chdir(cwd)
def main(): """ main """ arguments = IArguments(__doc__) content = open(arguments.filepath).read() open(arguments.filepath + ".bak", "w").write(content) try: newcontent = transliterate(content) write_newcontent(arguments.filepath, newcontent) except UnicodeEncodeError as ex: console(str(ex), color="red") newcontent = forceascii(content) write_newcontent(arguments.filepath, newcontent)
def print_error_coll(coll): """ @type coll: list @return: None """ for f in coll: for es in f: es = str(es) if "\n" in es: for i in es.split("\n"): print(i) else: console(es)
def main(): """ main """ if os.path.exists(".DS_Store"): os.remove(".DS_Store") try: pics_moved = 0 media_files = check_for_existence(read_path()) print(len(media_files)) for filepath in media_files: # if pics_moved > 0: # raise SystemError() if not valid_types(filepath): print("not valid type", filepath) else: (year, month, day) = determine_date_file(filepath) is_image = fp_is_jpg(filepath) # noinspection PyBroadException try: if is_image: (year, month, day) = exif_date_time(filepath, year, month, day) except: print ('hoi') raise (year, month, day) = determine_date_filename_dropbox(filepath, year, month, day) day_path = ensure_directory(year, month, day) #print(day_path + "/" + os.path.basename(filepath), os.path.exists(day_path + "/" + os.path.basename(filepath))) if not os.path.exists(day_path + "/" + os.path.basename(filepath)): print(filepath) os.system('mv ' + shell_escape(filepath) + ' ' + shell_escape(day_path) + "/") pics_moved += 1 print() print(pics_moved, "pics moved") return pics_moved except AppInstanceRunning: console(color="red", msg="instance runs already")
def sort_arguments(arguments): """ @type arguments: dict @return: tuple """ opts = {} posarg = {} for k in arguments: try: possnum = arguments[k] if isinstance(possnum, str): possnum = possnum.replace("'", "").replace('"', '') if "." in possnum: arguments[k] = float(possnum) else: arguments[k] = int(possnum) except ValueError: pass key = k.replace("pa_", "").replace("op_", "").strip() if len(key) > 0: if k.startswith("pa_"): posarg[k.replace("pa_", "")] = arguments[k] elif k.startswith("op_"): opts[k.replace("op_", "")] = arguments[k] else: posarg[k] = arguments[k] else: console("key not matched", key, color="red") return opts, posarg
Options: -h --help Show this screen. author : rabshakeh ([email protected]) project : consoleprinter created : 10-11-15 / 17:31 """ import os import sys from arguments import Arguments from consoleprinter import transliterate, forceascii, console if sys.version_info.major < 3: console("\033[31mpython3 is required\033[0m") exit(1) class IArguments(Arguments): """ IArguments """ def __init__(self, doc): """ __init__ """ self.help = False self.filepath = "" super().__init__(doc)
author : rabshakeh ([email protected]) project : forks created : 31 Jul 2016 / 14:17 where : Latitude: 51.825310 longitude: 4.650966 https://www.google.nl/maps/place/51.825310,4.650966 """ import sys import os from arguments import Arguments from consoleprinter import console from cmdssh import cmd_exec if sys.version_info.major < 3: console("Python 3 is required", color="red", plaintext="True") exit(1) class IArguments(Arguments): """ IArguments """ def __init__(self, doc): """ __init__ """ self.help = False self.input = "" super().__init__(doc)
def main(): """ main """ arg = IArgument(__doc__) keyword = arg.keyword targetdir = os.path.join(os.path.expanduser(arg.target_dir), arg.keyword.replace(" ", "_").replace("/", "")) min_stars = arg.min_stars clone = arg.clone g = Github(arg.username, arg.password) results = [] cnt = 0 for p in g.search_repositories(keyword, "stars"): console("checking", p.name, color="grey") results.append(p) if len(results) % 100 == 0: time.sleep(1) rpath = os.path.join(targetdir, p.name) if os.path.exists(rpath): console(p.stargazers_count, p.name, "skipped", color="blue") else: try: if clone: if not os.path.exists(targetdir): call_command("mkdir " + targetdir) os.chdir(os.path.expanduser(targetdir)) commitobj = p.get_branch("master").commit commitobj.update() updated_at = commitobj.last_modified updated = parse(str(updated_at)) now = pytz.utc.localize(datetime.datetime.now()) except GithubException as e: console(e.data["message"], color="red") if "API rate limit exceeded" in str(e): time.sleep(120) now = datetime.datetime.now() updated_at = p.updated_at updated = parse(str(updated_at)) td = now - updated updated = td.total_seconds() / 60 / 60 # noinspection PyUnresolvedReferences if updated < arg.numdays * 24: # console(p) if p.stargazers_count < min_stars: break else: cnt += 1 if clone: output = p.name + " " + str(Repo.clone_from(p.clone_url, p.name).active_branch) + " cloned" console(cnt, p.stargazers_count, output, str(updated_at), color="green") # console("skipp", str(p.name), color="blue") else: if os.path.exists(rpath): print("del", rpath) if clone: call_command("rm -Rf " + rpath) # print(g.get_user().add_to_starred(p)) for r, ds, f in os.walk(arg.target_dir): if r != arg.target_dir and os.path.dirname(r) == arg.target_dir: if len(f) == 0 and len(ds) == 0: os.rmdir(r) elif len(ds) == 0: printf = True for fw in f: if not fw.startswith("."): printf = False if printf: for fw in f: os.remove(os.path.join(r, fw)) os.rmdir(r)
def main(): """ main """ arguments = IArguments(__doc__) def endswith(extension): """ @type extension: str @return: None """ return arguments.input.lower().endswith(extension) print("\033[91mopen: {}\033[0m".format(arguments.input)) if "dash://" in arguments.input: os.system("/usr/bin/open " + arguments.input) elif "http" in arguments.input and not endswith("md"): os.system("osascript -e 'tell application \"Google Chrome\" to open location \"" + arguments.input + "\"'") os.system("osascript -e 'tell application \"Google Chrome\" to activate';") return else: arguments.input = os.path.abspath(os.path.expanduser(str(arguments.input))) alternative = "" if not os.path.exists(arguments.input): console(arguments.input, "not found", color="red") alts = os.popen("mdfind " + os.path.basename(arguments.input) + " | grep " + os.path.basename(arguments.input).split(".")[0] + " | grep -v pyc 2> /dev/null").read() alts = [x.strip() for x in str(alts).split("\n") if x.strip()] alts.extend([os.path.join(os.getcwd(), x) for x in os.listdir() if x.strip() and os.path.basename(arguments.input).split(".")[0] in x]) if len(alts) > 0: alts = list(set(alts)) alts.sort(key=lambda x: len(x)) for alternative2 in alts: if query_yes_no("Try \033[34m" + alternative2 + "\033[96m instead?", default=False): alternative = alternative2 break else: return else: return if alternative: arguments.input = alternative console("opening", arguments.input, color="darkyellow") pycharm = False sublime = False if endswith("py"): if query_yes_no("Use sublime=Y(def), or pycharm=N?", default=True): pycharm = False sublime = True else: pycharm = True sublime = False elif endswith("html"): if os.path.exists(arguments.input): location = "file://" + os.path.join(os.getcwd(), arguments.input) else: location = arguments.input os.system("osascript -e 'tell application \"Safari\" to open location \"" + location + "\"'") time.sleep(0.2) os.system("osascript -e 'tell application \"Safari\" to activate';") elif os.path.isdir(arguments.input): if os.path.exists(os.path.join(arguments.input, "setup.py")): pycharm = True if pycharm: if os.path.exists(os.path.join(arguments.input, ".idea")): cmd = "cd '" + arguments.input cmd += "'&&/Applications/PyCharm.app/Contents/MacOS/pycharm '" + arguments.input + "'' > /dev/null 2> /dev/null &" else: cmd = "cd '" + os.path.dirname(arguments.input) cmd += "'&&/Applications/PyCharm.app/Contents/MacOS/pycharm " + os.path.dirname(arguments.input) + " > /dev/null 2> /dev/null &" ossystem(cmd) time.sleep(0.2) ossystem("osascript -e 'tell application \"Pycharm\" to activate'") elif sublime: cmd = '/usr/bin/open -a /Applications/Sublime\ Text.app ' + arguments.input ossystem(cmd) time.sleep(0.2) ossystem("osascript -e 'tell application \"Sublime\" to activate'") else: os.system("cd '" + os.path.dirname(arguments.input) + "'&&/usr/bin/open '" + arguments.input + "'")
Options: -h --help Show this screen. author : rabshakeh ([email protected]) project : devenv created : 30-09-15 / 20:57 """ import os import sys import time from arguments import Arguments from consoleprinter import console, query_yes_no if sys.version_info.major < 3: console("\033[31mpython3 is required\033[0m") exit(1) class IArguments(Arguments): """ IArguments """ def __init__(self, doc): """ __init__ """ self.help = False self.input = "" super().__init__(doc)
created : 05 Jun 2016 / 20:57 where : Latitude: 51.825439 longitude: 4.650873 https://www.google.nl/maps/place/51.825439,4.650873 """ import consoleprinter import os import requests import sys from arguments import Arguments from bs4 import BeautifulSoup from consoleprinter import console if sys.version_info.major < 3: console("Python 3 is required", color="red", plaintext="True") exit(1) class IArguments(Arguments): """ IArguments """ def __init__(self, doc): """ __init__ """ self.help = False self.quotefile = "" self.cache = False super().__init__(doc)
def __init__(self, doc=None, validateschema=None, argvalue=None, yamlstr=None, yamlfile=None, parse_arguments=True, persistoption=False, alwaysfullhelp=False, version=None, parent=None, python3only=False): """ @type doc: str, None @type validateschema: Schema, None @type yamlfile: str, None @type yamlstr: str, None @type parse_arguments: bool @type argvalue: str, None @return: None """ self.doprinthelp = False self.m_once = None self.write = None self.load = None self.m_schema = validateschema self.m_reprdict = {} self.m_doc = "" self.__add_parent(parent) self.parsedarguments = {} self.command = "" if python3only: require_python3_raise_runtime_error() if not hasattr(self, "validcommands"): self.validcommands = [] if parent is not None: if hasattr(parent, "help") and parent.help is True: if len(parent.positional["args"]) == 0: print() if doc is None: # noinspection PyUnresolvedReferences import __main__ console( "Arguments first param 'doc' is not set using __main__.__doc__ now", plaintext=True, color='orange') triggerword = "usage" console(remove_extra_indentation(__main__.__doc__, triggerword), plaintext=True) self.m_doc = remove_extra_indentation(__main__.__doc__, triggerword) if doc is not None: triggerword = "usage" newdoc = remove_extra_indentation(doc, triggerword) self.m_doc = self.reorder_commandlist(newdoc) self.m_argv = argvalue self.m_persistoption = persistoption self.m_alwaysfullhelp = alwaysfullhelp self.m_version = version if not hasattr(self, "m_commandline_help"): self.m_commandline_help = {} self.m_commandline_help_default = {} if yamlfile: self.from_yaml_file(yamlfile) elif yamlstr: self.from_yaml(yamlstr) elif parse_arguments is True: parsedok = False exdoc = False sysex = False try: self.parse_arguments(self.m_schema) parsedok = True except DocoptExit: exdoc = True raise except SystemExit: sysex = True raise finally: if parsedok is False and exdoc is False and sysex is False: print() if hasattr(self, "help") and getattr(self, "help") is True: # noinspection PyUnresolvedReferences if self.command and len(self.m_argv) > 1: # noinspection PyUnresolvedReferences if self.m_argv[ -2] is self.command and self.command in self.validcommands: self.print_commandline_help(usageonly=False) else: if self.doprinthelp: print("doprinthelp") self.print_commandline_help(usageonly=False) if self.m_parents is not None: raise SystemExit(0) else: self.print_commandline_help(usageonly=False) raise SystemExit(1) else: if exdoc is True: print(self.get_usage_from_mdoc()) raise SystemExit(1) if self.write is not None: fp = open(self.write, "w") self.write = "" fp.write(self.as_yaml()) self.write = fp.name fp.close() elif self.load is not None: self.from_yaml_file(self.load) if yamlfile: raise AssertionError("not implemented")
def clone_or_pull_from(remote, name, argument): """ @type remote: str @type name: str @type argument: Arguments @return: None """ cnt = 0 global dotprinted global dotlock while True: try: gp = join(join(join(expanduser("~"), "workspace"), "github"), name) if exists(gp): r = Repo(gp) origin = r.remote() origin.fetch() if argument.command == "new": sys.stdout.write("\033[30m@\033[0m") else: try: origin.pull() except GitCommandError as ex: console(".", plaintext=True, color="blue") console("gp", gp) console_exception(ex) console("sleep 1, reset, and retry") time.sleep(1) try: os.system( "cd " + gp + "&&git reset --hard origin/master&&git clean -f" ) origin.pull() except GitCommandError as ex: console(".", plaintext=True, color="blue") console("gp", gp) console(str(ex), "giving up", color="red") try: dotlock.acquire() if dotprinted is True: sys.stdout.write("\033[30m.\033[0m") sys.stdout.flush() dotprinted = True finally: dotlock.release() # ret = name + " " + str(r.active_branch) + " pulled" else: newrepos = join( join(join(expanduser("~"), "workspace"), "github"), os.path.dirname(name)) if not exists(newrepos): os.mkdir(newrepos) newrepos = join( join(join(expanduser("~"), "workspace"), "github"), name) if not exists(newrepos): os.mkdir(newrepos) ret = name + " " + str( Repo.clone_from(remote, newrepos).active_branch) + " cloned" try: dotlock.acquire() if dotprinted is True: dotprinted = True ret = "\n" + ret finally: dotlock.release() #print("\033[32m" + ret + "\033[0m") newreposlink = join( join(join(expanduser("~"), "workspace"), "github"), "_newrepos") if not exists(newreposlink): os.mkdir(newreposlink) newreposlink = join(newreposlink, os.path.dirname(name)) if not exists(newreposlink): os.mkdir(newreposlink) newreposlinksym = newreposlink + "/" + os.path.basename(name) os.system("ln -s " + newrepos + " " + newreposlinksym) except Exception as e: console_exception(e) cnt += 1 if cnt > 3: break finally: break return True
def main(): """ main """ arguments = IArguments(__doc__) timestamp = datetime.datetime.now().strftime("%A %d %B %Y (week;%W day;%j), %H:%M:%S").replace(";0", ":").replace(";", ":") if not os.path.isdir(arguments.rootpath): console(arguments.rootpath, " is not a folder", color="red") return gitdirs = [] lg = os.walk(arguments.rootpath) for i in lg: if i[0].lower().endswith(".git"): if len(gitdirs) % 20 == 0: print(len(gitdirs), "items") gitdirs.append(i[0]) if arguments.gitcommand == "status": for gd in gitdirs: print("\033[34m" + gd + "\033[0m") result = os.popen("cd " + os.path.dirname(gd) + "&&git status ").read() if "modified" in result or "new file" in result or "untracked" in result: print("\n\033[93mchanged: " + os.path.dirname(gd), "\033[0m") print_line(str(result).strip()) if query_yes_no("Continue(y) or exit(n)?", force=arguments.force): pass else: exit(1) elif arguments.gitcommand == "gitreset": if query_yes_no("are you sure?", force=arguments.force): for gd in gitdirs: print("\033[34m" + gd + "\033[0m") os.system("cd " + os.path.dirname(gd) + "&&git reset --hard origin/master; git clean -f") elif arguments.gitcommand == "gconf": for gd in gitdirs: print("\033[34m" + gd + "\033[0m") os.system("cd " + os.path.dirname(gd) + "&&gconf") elif arguments.gitcommand == "commit": for gd in gitdirs: print("\033[34m" + gd + "\033[0m") os.system("cd " + os.path.dirname(gd) + "&&git commit -am '-';") elif arguments.gitcommand == "pull": for gd in gitdirs: print("\033[34m" + gd + "\033[0m") os.system("cd " + os.path.dirname(gd) + "&&git pull") elif arguments.gitcommand == "push": for gd in gitdirs: print("\033[34m" + gd + "\033[0m") os.system("cd " + os.path.dirname(gd) + "&&git push") elif arguments.gitcommand: for gd in gitdirs: print("\033[34m" + gd + "\033[0m") os.system("cd " + os.path.dirname(gd) + "&&" + arguments.gitcommand) print("\033[33m{}\033[0m".format(timestamp))
def run_unit_test(class_name=None, methodname=None, caller_globals=None, failfast=True, profile=False, quiet=True): """ @type class_name: str, unicode, None @type methodname: str, unicode, None @type caller_globals: str, unicode, None @type failfast: bool @type profile: bool @type quiet: bool @return: None """ # clear_screen() suite = unittest.TestSuite() if failfast is None: failfast = True if methodname and not class_name: for i in caller_globals: if isinstance(caller_globals[i], type): if issubclass(caller_globals[i], unittest.TestCase): for m in dir(caller_globals[i]): if methodname == m: if class_name: console("found another", m, "in", i, color="red") a = eval( input( "would you like to use this one? (y/n=default): " )) if a.strip().lower() == "y": class_name = i else: if quiet is False: console("found", m, "in", i, color="cyan") class_name = i if not class_name: raise ValueError("run_unit_test:cannot find class for method") cl = [os.path.basename(os.getcwd())] if methodname and class_name: cl.append(class_name + ":" + methodname) elif class_name: cl.append(class_name) elif methodname: cl.append(methodname) if failfast is True: if quiet is False: cl.append("failing fast") if class_name != "_*_": if len(cl) > 0: if quiet is False: console(*cl, color="cyan") if methodname and class_name: cls = caller_globals[class_name] suite.addTest(cls(methodname)) else: if class_name is not None: suite = unittest.TestLoader().loadTestsFromTestCase( caller_globals[class_name]) else: if "TESTDIR" in os.environ: suite = unittest.TestLoader().discover(os.environ["TESTDIR"]) else: suite = unittest.TestLoader().discover(".") try: # noinspection PyProtectedMember if len(suite._tests) == 0: console_warning("Can't find tests, looked in test*.py") except BaseException as e: console_warning(e) profiletrace = None if profile is True: profiletrace = start_profile() if quiet is True: sbuffer = "" result = unittest.TextTestRunner(failfast=failfast, stream=open("/dev/null", "w"), buffer=sbuffer).run(suite) else: result = unittest.TextTestRunner(failfast=failfast).run(suite) if profiletrace is not None: end_profile(profiletrace, items=50) return result
def unit_test_main(caller_globals=None): """ @type caller_globals: dict """ parser = OptionParser() parser.add_option("-c", "--class", dest="classname", default=None, help="UnitTest classname") parser.add_option("-m", "--method", dest="method", default=None, help="UnitTest methodname (optional)") parser.add_option("-f", "--failfast", dest="failfast", action="store_true", help="Fail fast") parser.add_option("-a", "--all", dest="all", action="store_true", help="Run all test without stop on fail") parser.add_option("-s", "--showclasses", dest="showclasses", action="store_true", help="Show all classes") parser.add_option("-r", "--resetconsole", dest="resetconsole", action="store_true", help="Reset console (clear screen)") parser.add_option("-p", "--profile", dest="profile", action="store_true", help="Profile the method", default=False) parser.add_option("-q", "--quiet", dest="quiet", action="store_true", help="Silence output except when there is an error", default=False) (options, args) = parser.parse_args() if options.resetconsole: clear_screen() if options.all is None: options.all = True if options.quiet is None: options.quiet = True if options.all is True and options.failfast is None: options.failfast = False stderr = sys.stderr stdout = sys.stdout if options.quiet is True: sys.stdout = open("/dev/null", "w") sys.stderr = open("/dev/null", "w") if options.showclasses: options.classname = "_*_" try: methods = [] if options.method is not None: options.method = options.method.replace(" ", "") if "," in options.method: methods = options.method.split(",") results = [] if len(methods) > 0: for m in methods: result = run_unit_test(options.classname, m, caller_globals, options.failfast, options.profile, quiet=options.quiet) results.append(result) else: result = run_unit_test(options.classname, options.method, caller_globals, options.failfast, options.profile, quiet=options.quiet) results.append(result) sys.stdout = stdout sys.stderr = stderr if options.quiet is True: for r in results: if len(r.errors) > 0: coll = r.errors print_error_coll(coll) if len(r.failures) > 0: coll = r.failures print_error_coll(coll) except (KeyError, ValueError) as e: testclasses = [] mcnt = 1 for i in caller_globals: if isinstance(caller_globals[i], type): if issubclass(caller_globals[i], unittest.TestCase): testclasses.append(i) for m in dir(caller_globals[i]): if "test_" in m: console(mcnt, i + ":" + m, color="green") mcnt += 1 for i in testclasses: console(i, color="blue") if isinstance(e, KeyError): if options.classname != "_*_": console_warning("Class not found", options.classname) elif isinstance(e, ValueError): if options.classname: console_warning("Method not found", options.classname, options.method) else: console_warning("Method not found", options.method)
def parse_arguments(self, schema=True): """ @type schema: Schema @return: None """ arguments = None if schema is not None: self.m_schema = schema if self.load is None: if self.m_persistoption is True: optsplit = self.m_doc.split("Options:") optsplit[0] += "Options:\n" optsplit[ 0] += """ -w --write=<writeymlpath>\tWrite arguments yaml file.\n -l --load=<loadymlpath>\tLoad arguments yaml file.""" self.m_doc = "".join(optsplit) console(optsplit) try: if self.m_argv is None: self.m_argv = sys.argv[1:] if "\\" in str(self.m_argv): self.m_argv = [ x for x in "".join([x + "@$" for x in self.m_argv]).replace( '\@$', ' ').split('@$') if x ] sorted_argv = [] options_argv = [] next_is_option = False for argvitem in self.m_argv: if next_is_option is True: options_argv.append(argvitem) next_is_option = False else: if str(argvitem).startswith( "-") and not str(argvitem).startswith("--"): options_argv.append(argvitem) next_is_option = True else: sorted_argv.append(argvitem) flattened_sorted_argv = [] if len(options_argv) > 0: if len(sorted_argv) > 0: sorted_argv.insert(len(sorted_argv) - 1, options_argv) else: sorted_argv.insert(0, options_argv) flattened(sorted_argv, flattened_sorted_argv) # self.m_argv = flattened_sorted_argv if self.m_parents: for parent in self.m_parents: if parent.command: # pass # console_warning("removed parent") self.m_argv.remove(parent.command) arguments = dict( docopt(self.m_doc, self.m_argv, options_first=False, version=self.m_version)) self.parsedarguments = arguments.copy() if "--help" in [ s for s in arguments.keys() if isinstance(s, str) ] or "-h" in [ s for s in arguments.keys() if isinstance(s, str) ]: self.doprinthelp = True except DocoptExit: if self.m_alwaysfullhelp is True: for sarg in list(sys.argv): if "-h" in sarg or "--help" in sarg: self.print_commandless_help() exit(1) else: usage = self.get_usage_from_mdoc() print("\033[34m" + usage + "\033[0m") raise SystemExit(0) else: if "-h" in self.m_argv or "--help" in self.m_argv: self.print_commandless_help() raise SystemExit(0) else: raise k = "" try: if isinstance(arguments, dict): for k in arguments: trypath = False if isinstance(arguments[k], str): trypath = "~" in arguments[k] or "/" in arguments[k] if trypath is False: trypath = "folder" in k or "path" in k if trypath: if hasattr(arguments[k], "replace"): arguments[k] = arguments[k].replace( "~", expanduser("~")) if arguments[k].strip() == ".": arguments[k] = os.getcwd() if "./" in arguments[k].strip(): arguments[k] = os.path.abspath( arguments[k]) if arguments[k].rstrip("/").strip() != "/": arguments[k] = arguments[k].rstrip( "/").strip() if hasattr(arguments[k], "strip") and arguments[k].strip() == "": arguments[k] = "/" except AttributeError as e: console("Attribute error:" + k.strip(), "->", str(e), color="red") if isinstance(arguments, dict): for k in arguments: console(k.strip(), color="red") handle_ex(e) else: if isinstance(self.load, str) and self.load is not None: loaded_arguments = yaml.load(open(str(self.load))) arguments = {} for k in loaded_arguments["options"]: arguments["op_" + k] = loaded_arguments["options"][k] for k in loaded_arguments["positional"]: arguments["pa_" + k] = loaded_arguments["positional"][k] else: console("self.load is not a str", self.load, color="red") try: if not isinstance(arguments, dict): raise AssertionError("arguments should be a dict by now") if "--" in arguments: del arguments["--"] validate_arguments = dict( (x.replace("<", "").replace(">", "").replace("--", "").replace( "-", "_"), y) for x, y in arguments.items()) if self.m_schema is not None: schema_keys = self.m_schema.get_keys() for k in list(validate_arguments.keys()): if k not in schema_keys: self.m_schema.add_void_schema_item(k) self.m_schema.validate(validate_arguments) arguments = dict((x.replace("<", "pa_").replace(">", "").replace( "--", "op_").replace("-", "_"), y) for x, y in arguments.items()) except SchemaError as e: name = self.get_command_path() abort(name, "".join([x for x in e.errors if x])) print() print(self.m_doc.strip()) raise options, positional_arguments = self.sort_arguments(arguments) self._set_fields(positional_arguments, options) checking_commands = False for line in self.m_doc.split("\n"): if line.strip().lower().startswith("commands:"): checking_commands = True if checking_commands is True: ls = [x for x in line.split() if x] if len(ls) > 1: command = ls[0].strip() if command not in self.m_commandline_help and command not in [ "author", "date", "project" ]: if command not in self.m_commandline_help_default: self.m_commandline_help_default[command] = str( " ".join(ls[1:])).strip()
def call_command(command, cmdfolder=os.path.expanduser("~"), verbose=False, streamoutput=True, returnoutput=False, prefix=None, ret_and_code=False): """ @type command: str, unicode @type cmdfolder: str, unicode @type verbose: bool @type streamoutput: bool @type returnoutput: bool @type prefix: str, None @type ret_and_code: bool @return: None """ try: if ret_and_code is True: streamoutput = False returnoutput = True if verbose: console(cmdfolder, command, color="yellow") for prevfile in os.listdir(cmdfolder): if prevfile.startswith("callcommand_"): os.remove(os.path.join(cmdfolder, prevfile)) commandfile = "callcommand_" + hashlib.md5( str(command).encode()).hexdigest() + ".sh" commandfilepath = os.path.join(cmdfolder, commandfile) open(commandfilepath, "w").write(command) if not os.path.exists(commandfilepath): raise ValueError("commandfile could not be made") try: os.chmod(commandfilepath, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC) proc = subprocess.Popen(commandfilepath, stderr=subprocess.PIPE, stdout=subprocess.PIPE, cwd=cmdfolder, shell=True) retval = "" if streamoutput is True: while proc.poll() is None: time.sleep(0.1) output = proc.stdout.readline() if isinstance(output, bytes): output = output.decode() if len(remove_escapecodes(output).strip()) > 0: if returnoutput is True: retval += str(output) if prefix is None: prefix = command if len(prefix) > 50: prefix = prefix.split()[0] console(output.rstrip(), color="green", prefix=prefix) if returnoutput is True: retval += output so, se = proc.communicate() if ret_and_code is False and (proc.returncode != 0 or verbose): so = so.decode().strip() se = se.decode().strip() output = str(so + se).strip() if proc.returncode == 1: if verbose: console_error(command, CallCommandException("Exit on: " + command), errorplaintxt=output, line_num_only=9) else: raise CallCommandException("Exit on: " + command + " - " + output) else: console("returncode: " + str(proc.returncode), command, color="red") if ret_and_code is True or returnoutput is True and streamoutput is False: retval = so retval += se if hasattr(retval, "decode"): retval = retval.decode() if ret_and_code is True: returnoutput = False if returnoutput is True: return retval.strip() elif ret_and_code is True: return proc.returncode, retval.rstrip() else: return proc.returncode finally: if os.path.exists(commandfilepath): #if proc.returncode == 0: os.remove(commandfilepath) except ValueError as e: console_exception(e) except subprocess.CalledProcessError as e: console_exception(e)
def main(): """ main """ require_python3() forks = set() write_file = True projects_set = set() excludes = [] workspacefolder = os.path.join(os.path.expanduser("~"), "workspace") if os.path.exists(os.path.join(workspacefolder, ".gitutilsexclude")): excludes.extend([os.path.join(workspacefolder, x.strip()) for x in open(os.path.join(workspacefolder, ".gitutilsexclude")).read().split("\n") if x.strip()]) wsfolders = [os.path.join(workspacefolder, folder) for folder in os.listdir(workspacefolder) if os.path.join(workspacefolder, folder) not in excludes] print(len(wsfolders)) new_projects = [] for wsfolder in wsfolders: for root, dirlist, files in os.walk(wsfolder): if root.endswith(".git"): print_stdout(".") if os.path.exists(root + "/config"): config = open(root + "/config").read() if len(config.split("url ="))==1: print("\nno url found", root) else: config = config.split("url =")[1].split("\n")[0].strip() if root.strip("/.git") not in excludes: new_projects.append('("' + os.path.dirname(root) + '", "' + config + '")') project_dir = root.strip("/.git/") project_base_folder = os.path.basename(os.path.dirname(project_dir)) if project_base_folder == "forks": project_name = os.path.basename(project_dir) for project_name_iter in projects_set: if project_name in project_name_iter: forks.add(project_name_iter) for fork in forks: projects_set.remove(fork) if write_file: afile = open(os.path.expanduser("~") + "/workspace/git_utils/exclude_dirs", "wt") for projectname in projects_set: afile.write(str(projectname) + "\n") afile.close() new_projects = [project_name for project_name in new_projects if "workspace/github" not in project_name] new_projects2 = [] for d in new_projects: if eval(d)[0] not in excludes: new_projects2.append(d) print new_projects=new_projects2 print() console(len(new_projects), "projects", color='blue', fileref=False) if len(new_projects) > 0: proj_list_imports = g_checkout + "\n\n" proj_list_imports += "def main():\n \"\"\"\n main\n \"\"\"\n" proj_list_header = " project_list = [" numspaces = len(proj_list_header) proj_list = "" first = True spaces = "" for new_project in new_projects: if first: spaces = " " * numspaces project = spaces + str(new_project) + ",\n" project = project.replace("\", \"", "\",\n" + spaces + " \"") proj_list += project first = False proj_list = proj_list.strip().strip(",") proj_list += "]" proj_list_imports = proj_list_imports.replace("import Repo", "import Repo\n\n") proj_list_imports = proj_list_imports.replace("from git", "\nfrom git") g_driver = g_drive_main.replace("if __name__", "\n\nif __name__") output = proj_list_imports + proj_list_header + proj_list + "\n" + g_driver open(os.path.expanduser("~/workspace/devenv_private/current_workspace.py"), "wt").write(output.strip()+"\n")