def _open_lock(self): if self._lock_file: raise Already_Open try: fn = Filename._as_file(self.file_name) fname = Filename(fn + ".lock", absolute=1) self._can_lock = sos.access(fname.directory, sos.W_OK) if self._can_lock: self._lfile_name = name = fname.name self._lock_file = sos.open(name, sos.O_CREAT | sos.O_EXCL) except sos.error as exc: if exc.args[0] != errno.EEXIST: raise raise Sync_Conflict(self.file_name)
def handler (self, cmd) : i_enc = cmd.input_encoding tfmt = self.time_fmt = cmd.time_format for arg in cmd.argv : if arg == "-" : arg = "STDIN" output = Filename (".pdf", cmd.Output or arg).name if arg == "STDIN" : file_time = time.localtime () fn = output txt_in = sys.stdin.read () else : file_time = time.localtime (sos.path.getmtime (arg)) fn = self._display_filename (cmd, arg) with open (arg, "rb") as fi : txt_in = fi.read () txt = Untabified \ (pyk.decoded (txt_in, i_enc).lstrip ("\n\f").rstrip ()) ft = time.strftime (tfmt, file_time) options = self._pdf_opts (cmd, arg, fn, ft, output) if cmd.show_options : from _TFL.formatted_repr import formatted_repr print (formatted_repr (options)) ofn = options ["output"] pdf_doc = PDF_Doc (txt, ** options) if cmd.Display : subprocess.run ([cmd.display_program, ofn]) elif cmd.Print : subprocess.run ([cmd.print_program, ofn]) if cmd.purge_pdf and (cmd.Display or cmd.Print) : sos.unlink (ofn) elif cmd.verbose : print ("Created file", ofn)
def _open_w(self, mode, bufsize, backup_name): """Open locked file for writing""" curr_key, tmp_name = self._get_key_save() result = None try: if curr_key == self.last_key: if curr_key: if backup_name: bak_name = Filename(backup_name, self.file_name).name self.backup_name = bak_name sos.rename(tmp_name, bak_name) else: self.backup_name = None sos.remove(tmp_name) result = open(self.file_name, mode, bufsize) self.exists = 1 else: self.changed_key = curr_key raise Sync_Conflict(self) finally: if not result: sos.rename(tmp_name, self.file_name) else: self._write_header(result) return result
def rewrite (self, fname, target_dir) : target = Filename (target_dir, fname) f = open (fname, "r") o = open (target, "w") print ("Processing %s" % (target, )) for l in self (f) : o.write (l)
def __init__(self, sync_dir, mode, bufsize, bak_name): self.__Ancestor.__init__(self, sync_dir, mode, "w", bufsize, bak_name) name = self.sync_dir.file_name if sos.path.exists(name): if bak_name: name = sos.tempfile_name \ (Filename (name).directory or sos.curdir) sos.mkdir(name) else: sos.rmdir(name, deletefiles=1) if sos.path.exists(name): raise Could_Not_Delete_Old_DB \ ( "Deleting %s failed without error message from " "the OS. Try saving to a different database." % (name, ) ) try: sos.mkdir(name) except (IOError, OSError): raise Could_Not_Delete_Old_DB \ ( "Deleting %s failed without error message from " "the OS. Try saving to a different database." % (name, ) ) else: sos.mkdir(name) self.name = name self._write_stamp_file_header(name)
def import_module (path) : fn = Filename (path) mn = fn.base md = fn.directory or "./" with TFL.Context.list_push (sys.path, md, 0) : result = importlib.import_module (mn) result.__App_Tests_Dir__ = md return result
def _gen(parent, src_dir): for f in sos.expanded_globs(pjoin(src_dir, "*.txt")): info = _page_info(f) if info: n = info.get("name", f) info["perma_name"] = base = Filename(n).base info["name"] = name = "%s.html" % (base, ) yield GTW.RST.TOP.Page_ReST \ (parent = parent, src_dir = src_dir, ** info)
def _gen(l, m, r, file_name, file_time): now = time.strftime(self.time_fmt, time.localtime()) for x in (l, m, r): if x == "$b": x = Filename(file_name).base elif x == "$n": x = file_name elif x == "$t": x = now yield x
def _pdf_font (self, font_name) : if font_name in ("Helvetica", "Courier", "Times Roman") : result = font_name else : cache = self._font_cache result = Filename (font_name).base if result not in cache : cache [result] = font = TTFont (result, font_name) pdfmetrics.registerFont (font) return result
def Page_ReST_F(parent, src_dir, name, **kw): src_path = pjoin(src_dir, Filename(".txt", name).name) src_contents = _file_contents(src_path) return GTW.RST.TOP.Page_ReST \ ( parent = parent , src_dir = src_dir , name = name , src_contents = src_contents , ** kw )
def _find_import (self, imported, prefix = "") : if imported in self.seen : return True else : self.seen [imported] = True pym = self.path_of (imported, prefix) if pym : file = Filename (pym.path_name, absolute = True) if file.base not in self.ignore : if self._add (pym) : self._find_imports (pym.path_name, imported, pym.pkg) return True
def diff(self, new_content): if self != new_content: old = pyk.text_type(self) new = pyk.decoded(new_content, *self.encodings) diffs = self._diffs \ (old, new, fromfile = self.file_name, tofile = "Result of test") diff_no = len(diffs) d_name = Filename(".diff", self.file_name).name with open(d_name, "wb") as f: f.write(pyk.encoded("\n".join(diffs))) return "%d difference%s (see %s)" % \ (diff_no, "" if diff_no == 1 else "s", d_name)
def _open_lock (self) : if self._lock_file : raise Already_Open try : fn = Filename._as_file (self.file_name) fname = Filename (fn + ".lock", absolute = 1) self._can_lock = sos.access (fname.directory, sos.W_OK) if self._can_lock : self._lfile_name = name = fname.name self._lock_file = sos.open (name, sos.O_CREAT | sos.O_EXCL) except sos.error as exc : if exc.args [0] != errno.EEXIST : raise raise Sync_Conflict (self.file_name)
def resolved (x) : result = None if x == "b" : result = Filename (file_name).base elif x == "c" : result = now elif x == "n" : result = file_name elif x == "m" : result = file_time elif x == "s" : result = subject elif x == "t" : result = title return result
def _get_key_save(self): ### get current key of file ### to make it save, rename the file first and read key afterwards tmp_name = sos.tempfile_name \ (Filename (self.file_name).directory or sos.curdir) curr_key = "" try: sos.rename(self.file_name, tmp_name) curr_key = self._get_key(tmp_name) except sos.error as exc: if exc.args[0] != errno.ENOENT: raise tmp_name = None except (SystemExit, KeyboardInterrupt, IOError) as exc: raise except: pass return curr_key, tmp_name
def _footer_fields \ (self, footer, file_name, file_time, subject, title, now) : l, m, r = "", "", "" if footer.strip () : l, _, x = split_hst (footer, "|") if _ : m, _, r = split_hst (x, "|") if m and not _ : m, r = r, m l, m, r = tuple \ ( self._fields__gen (l, m, r, file_name, file_time, subject, title, now) ) return \ ( l if l != ":" else (Filename (file_name).base if title else "") , m if m != ":" else file_time , r if r != ":" else now )
def _create_config_uwsgi (self, cao, config_options, templateer) : P = self._P (cao) app_dir = sos.path.dirname (self._app_path (cao, P)) macro = templateer.GTW.get_macro \ ("uwsgi_config_ini", templ_name = config_options ["templ_name"]) script_name = Filename (".ini", cao.script_path, absolute = True) c_path = script_name.name \ if script_name.base not in ("-", "stdout") else "" plugin = cao.plugin if not plugin : plugin = "python3" if sys.version_info >= (3, ) else "python" socket = config_options ["socket"] if socket.startswith ("unix:") : socket = socket [5:] stats_server = config_options ["stats_server"] if stats_server.startswith ("unix:") : stats_server = stats_server [5:] kw = dict \ ( chdir = script_name.directory , chmod_socket = "660" , chown_socket = "%s:%s" % (cao.user or cao.http_user, cao.http_user) , config_name = c_path , gid = cao.group or cao.http_user , lazy_apps = ["false", "true"] [cao.lazy_apps] , master = "true" , module = script_name.base , need_threads = "true" , plugin = plugin , processes = cao.processes , py_path = reversed ([pyk.decoded (app_dir)] + P.py_path.split (":")) , server_name = config_options ["server_name"] , socket = socket , stats_server = stats_server , threads = 0 , uid = cao.user or cao.http_user , virtualenv = cao.virtualenv ) config = macro (** kw) config_s = strip_empty_lines (config) self._write_config \ (cao, c_path, config_s, "Created uwsgi config")
def _handler(self, sub, cmd): time_format = _Sub_Command_.time_fmt = cmd.time_format i_enc = cmd.input_encoding o_enc = cmd.output_encoding pbl.env["LC_ALL"] = "en_US.%s" % o_enc.replace("-", "") for arg in cmd.argv: if arg in ("-", "STDIN"): file_time = time.localtime() fn = "STDIN" txt_in = sys.stdin.read() else: file_time = time.localtime(sos.path.getmtime(arg)) fn = Filename(arg).relative_to("~/") with open(arg, "rb") as fi: txt_in = fi.read() txt = txt_in.decode(i_enc).lstrip("\n").rstrip() ft = time.strftime(time_format, file_time) txt_out = self._encoded(txt, o_enc) options = tuple \ (pyk.encoded (o) for o in sub.options (cmd, fn, ft)) pbl_cmd = sub.pbl_cmd.__getitem__(options) with open_tempfile() as (fo, no): fo.write(txt_out) fo.close() cx = pbl_cmd[no] if cmd.verbose or cmd.dry_run: print(pbl_cmd, arg, "[%s]" % no) if not cmd.dry_run: if cmd.Display: if cmd.Print: print \ ( "Specify either -Display or -Print, " "but not both" ) raise SystemExit(10) with open_tempfile() as (fd, nd): fd.write(cx()) fd.close() pbl[cmd.display_program][nd]() else: cx()
def lock_file (file_name) : """Context manager providing a lock file.""" ln = Filename (file_name + ".lock", absolute = 1) try : ### Don't try to open a lock file in a read-only directory (don't ### need a lock there anyway!) can_lock = sos.access (ln.directory, sos.W_OK) if can_lock : lf = sos.open (ln.name, sos.O_CREAT | sos.O_EXCL) except sos.error as exc : if exc.args [0] != errno.EEXIST : raise raise Sync_Conflict (file_name) else : try : yield None finally : if can_lock : try : sos.close (lf) finally : sos.remove (ln.name)
def _response_body(self, resource, request, response): from pyspkac.spkac import SPKAC from M2Crypto import EVP, X509 top = resource.top HTTP_Status = top.Status ca_path = top.cert_auth_path if not ca_path: raise HTTP_Status.Not_Found() try: ### Unicode argument to `EVP.load_key` fails [M2Crypto==0.21.1] cert = X509.load_cert(Filename(".crt", ca_path).name) pkey = EVP.load_key(str(Filename(".key", ca_path).name)) except Exception: raise HTTP_Status.Not_Found() req_data = request.req_data spkac = req_data.get("SPKAC", "").replace("\n", "") if not spkac: exc = HTTP_Status.Bad_Request \ ( _T( "The parameter `SPKAC` is missing from the request. " "Normally, the web browser should supply that " "parameter automatically." ) ) resource.send_error_email(request, exc) raise exc challenge = request.session.get("spkac_challenge") desc = req_data.get("desc") email = request.user.name cn = "%s [%s]" % (email, desc) if desc else email ### Unicode arguments to `X509.new_email` fail [M2Crypto==0.21.1] X = X509.new_extension x1 = X(b"basicConstraints", b"CA:FALSE", critical=True) x2 = X \ ( b"keyUsage" , b"digitalSignature, keyEncipherment, keyAgreement" , critical = True ) x3 = X(b"extendedKeyUsage", b"clientAuth, emailProtection, nsSGC") s = SPKAC \ ( spkac, challenge, x1, x2, x3 , CN = cn , Email = email , O = cert.get_subject ().O ) scope = top.scope CTM = scope.Auth.Certificate start = CTM.E_Type.validity.start.now() finis = start + datetime.timedelta(days=365 * 2) c_obj = CTM(email=email, validity=(start, finis), desc=desc) scope.commit() result = c_obj.pem = s.gen_crt \ ( pkey, cert, c_obj.pid , not_before = self._timestamp (start) , not_after = self._timestamp (finis) ).as_pem () response.set_header \ ( "content-disposition", "inline" , filename = "%s.crt" % (email, ) ) scope.commit() return result
def _convert_filename (self, value) : fname = Filename (value, ".h").name if sys.platform == "win32" : fname = fname.replace (sos.sep, "/") return fname
def _display_filename (self, cmd, arg) : result = Filename (arg).relative_to ("~/") if result != arg : result = "~/" + result return result
def _main (cmd) : cmd_path = list (cmd.path or []) replacer = Re_Replacer (r"\.py[co]", ".py") a = cmd.argv [0] et = "" one_arg_p = len (cmd.argv) == 1 and not sos.path.isdir (a) if one_arg_p and not cmd.Extra_Interpreters : f = Filename (a) m = f.base py_version = " [py %s]" % \ ".".join (str (v) for v in sys.version_info [:3]) sys.path [0:0] = cmd_path mod_path = f.directory if f.directory else "./" if sos.path.exists \ (Filename ("__init__.py", default_dir = mod_path).name) : sys.path [0:0] = [sos.path.join (mod_path, "..")] sys.path [0:0] = [mod_path] flags = doctest.NORMALIZE_WHITESPACE if not cmd.nodiff : flags |= doctest.REPORT_NDIFF try : logging.disable (logging.WARNING) start = _timer () module = __import__ (m) module.expect_except = TFL.CAO.expect_except cases = len (getattr (module, "__test__", ())) or 1 f, t = doctest.testmod \ ( module , verbose = cmd.verbose , optionflags = flags ) exec_time = _timer () - start except KeyboardInterrupt : raise except Exception as exc : exec_time = _timer () - start if cmd.timing : et = " in %7.5fs" % (exec_time, ) msg = format_x % (replacer (a), py_version, exc, et) print (msg, file = sys.stderr) raise else : format = format_f if f else format_s if cmd.timing : et = " in %7.5fs" % (exec_time, ) print (replacer (format % TFL.Caller.Scope ()), file = sys.stderr) else : py_executables = [sys.executable] + list (cmd.Extra_Interpreters) py_version = "" head_pieces = sos.python_options () + \ [ __file__ , "-path %r" % (",".join (cmd_path), ) if cmd_path else "" ] for opt in ("nodiff", "timing", "verbose") : if getattr (cmd, opt) : head_pieces.append ("-" + opt) head = " ".join (hp for hp in head_pieces if hp) if cmd.summary : run_cmd = run_command_with_summary else : run_cmd = run_command if cmd.RExclude : x_pat = Regexp (cmd.RExclude) exclude = x_pat.search elif cmd.exclude : exclude = lambda a : fnmatch.fnmatch (a, cmd.exclude) else : exclude = lambda a : False def run_mod (a) : if exclude (a) : summary.excluded.append (a) print ("%s excluded" % (a, )) else : summary.modules += 1 for pyx in py_executables : run_cmd ("%s %s %s" % (pyx, head, a)) def run_mods (d) : for f in sorted (sos.listdir_exts (d, ".py")) : if has_doctest (f) : run_mod (f) if cmd.transitive : from _TFL.subdirs import subdirs def run_dir (d) : run_mods (d) for s in subdirs (d) : run_dir (s) else : run_dir = run_mods start = _timer () for a in cmd.argv : if sos.path.isdir (a) : run_dir (a) else : if has_doctest (a) : run_mod (a) if cmd.summary : format = format_f if summary.failed else format_s if cmd.timing : et = " in %7.5fs" % (_timer () - start, ) print ("=" * 79, file = sys.stderr) print \ ( format % TFL.Caller.Scope ( f = summary.failed , module = TFL.Record (__file__ = " ".join (cmd.argv)) , t = summary.total , cases = summary.cases , et = et ) , "[%s/%s modules fail]" % (len (summary.failures), summary.modules) , file = sys.stderr ) print \ ( " %s" % ("\n ".join ("%-68s : %s" % f for f in summary.failures)) , file = sys.stderr ) if summary.excluded : print \ (" %s excluded" % (", ".join (summary.excluded), ) , file = sys.stderr )
def _convert_filename(self, value): fname = Filename(value, ".h").name if sys.platform == "win32": fname = fname.replace(sos.sep, "/") return fname
def _create_config_http (self, cao, xxgi_macro_name, ** kw) : from _JNJ import JNJ import _JNJ.Templateer ssl_certificate = \ ( Filename ( cao.ssl_certificate, ".pem", "/etc/ssl/certs/" , absolute = True ).name if cao.ssl_certificate else "" ) ssl_certificate_chain = tuple \ ( Filename ( ssc, ".pem", "/etc/ssl/certs/" , absolute = True ).name for ssc in cao.ssl_certificate_chain ) ssl_certificate_key = \ ( Filename ( cao.ssl_certificate_key, ".key", "/etc/ssl/private/" , absolute = True ).name if cao.ssl_certificate_key else "" ) config_options = dict \ ( kw , address = cao.address , admin = cao.server_admin , aliases = cao.server_aliases , apache2_4 = cao.apache2_4 , app_root = cao.root_dir , ca_key_name = cao.ca_key_name , ca_path = cao.ca_path , cmd = cao , config_name = cao.config_path , doc_root = cao.document_root , extra_locations = dict (cao.GET ("extra_locations", {}), ** self._extra_locations) , group = cao.group , host_macros = cao.host_macro , HTTP2 = cao.HTTP2 , lib_dirs = cao.lib_dir , macro_name = "config_file" , macro_module = cao.macro_module , port = cao.port , script = cao.script_path , server_name = cao.server_name , ssl_certificate = ssl_certificate , ssl_certificate_chain = ssl_certificate_chain , ssl_certificate_key = ssl_certificate_key , ssl_ciphers = cao.ssl_ciphers , ssl_protocols = cao.ssl_protocols , templ_name = cao.macro_module , user = cao.user ) templateer = JNJ.Templateer \ ( encoding = cao.input_encoding , globals = dict (config_options = config_options) , load_path = cao.template_dirs , trim_blocks = True ) xxgi_macro = templateer.GTW.get_macro \ (xxgi_macro_name, templ_name = cao.macro_module) config_options.update (xxgi_macro = xxgi_macro) config = templateer.call_macro (** config_options) config_s = strip_empty_lines (config) c_path = cao.config_path self._write_config (cao, c_path, config_s, "Created http config") return config_options, templateer
class Module(TFL.SDG.C._Scope_): """Models a C module.""" Ancestor = TFL.SDG.C._Scope_ children_group_names = \ ( Ancestor.Decl , Ancestor.Head , Ancestor.Body , Ancestor.Tail ) header_comment_level = signature_level = 0 init_arg_defaults = dict \ ( author = None , header_comment = "" ) _autoconvert = dict \ ( header_comment = lambda s, k, v : s._convert_c_comment (k, v, eol = False) , name = lambda s, k, v : Filename (v).base ) star_level = 3 pass_scope = False _format_head = """ %(::*header_comment:)s %(::*signature:)s %(::*description:)s %(::*explanation:)s """.strip() _format_children = """ %(::*decl_children:)s %(::*head_children:)s %(::*body_children:)s %(::*tail_children:)s """ c_format = _format_head + """ """ + _format_children h_format = _format_head + """ #ifndef _%(name)s_h_ #define _%(name)s_h_ 1 """ + _format_children + """#endif /* _%(name)s_h_ */%(chr(10))s """ def __init__(self, *children, **kw): self.__super.__init__(*children, **kw) author = "" if self.author: author = " by %s" % self.author self.signature = self._convert_c_comment \ ( "signature" , "Module %s, written%s on %s" % ( self.name, author, time.strftime ("%a %d-%b-%Y %H:%M:%S", time.localtime (time.time ())) ) )