def _get_session(): global _session global _last_gc # Indexing some session data by the USER_HASH will help to avoid # maintaining sessions between two different databases. This could lead to # some errors in the POS in which the user making the sale does not exist. session_file = os.path.join( get_application_dir(), 'session-{}.db'.format(_get_user_hash())) if os.path.exists(session_file): with open(session_file, 'rb') as f: try: _session = pickle.load(f) except Exception: _session = {} else: _session = {} # From time to time remove old entries from the session dict now = localnow() if now - (_last_gc or datetime.datetime.min) > _expire_time: for k, v in list(_session.items()): if now - v['date'] > _expire_time: del _session[k] _last_gc = localnow() yield _session with open(session_file, 'wb') as f: pickle.dump(_session, f)
def _setup_venv(self): from stoqlib.lib.osutils import get_application_dir import venv stoqdir = get_application_dir("stoq") env_dir = os.path.join(stoqdir, 'venv') if not os.path.exists(env_dir): log.info('creating venv at %s', env_dir) if platform.system() == 'Windows': # On windows, pip will be included as an egg venv.create(env_dir, system_site_packages=True) else: venv.create(env_dir, system_site_packages=True, with_pip=True) log.info('creating venv done') # This is exactly what activate_this.py does old_os_path = os.environ.get('PATH', '') os.environ['PATH'] = os.path.join(env_dir, 'bin') + os.pathsep + old_os_path if sys.platform == 'win32': site_packages = os.path.join(env_dir, 'Lib', 'site-packages') else: site_packages = os.path.join(env_dir, 'lib', 'python%s' % sys.version[:3], 'site-packages') prev_sys_path = list(sys.path) import site site.addsitedir(site_packages) sys.real_prefix = sys.prefix sys.prefix = env_dir # Move the added items to the front of the path: new_sys_path = [] for item in list(sys.path): if item not in prev_sys_path: new_sys_path.append(item) sys.path.remove(item) sys.path[:0] = new_sys_path
def _create_eggs_cache(self): log.info("Creating cache for plugins eggs") # $HOME/.stoq/plugins default_store = get_default_store() path = os.path.join(get_application_dir(), 'plugins') if not os.path.exists(path): os.makedirs(path) existing_eggs = { unicode(os.path.basename(f)[:-4]): md5sum_for_filename(f) for f in glob.iglob(os.path.join(path, '*.egg'))} # Now extract all eggs from the database and put it where stoq know # how to load them for plugin_name, egg_md5sum in default_store.using(PluginEgg).find( (PluginEgg.plugin_name, PluginEgg.egg_md5sum)): # A little optimization to avoid loading the egg in memory if we # already have a valid version cached. if existing_eggs.get(plugin_name, u'') == egg_md5sum: log.info("Plugin %r egg md5sum matches. Skipping it..." % ( plugin_name, )) continue log.info("Creating egg cache for plugin %r" % (plugin_name, )) egg_filename = '%s.egg' % (plugin_name, ) plugin_egg = default_store.find( PluginEgg, plugin_name=plugin_name).one() with open(os.path.join(path, egg_filename), 'wb') as f: f.write(plugin_egg.egg_content)
def _prepare_logfiles(self): from stoqlib.lib.osutils import get_application_dir stoqdir = get_application_dir("stoq") log_dir = os.path.join(stoqdir, 'logs', time.strftime('%Y'), time.strftime('%m')) if not os.path.exists(log_dir): os.makedirs(log_dir) self._log_filename = os.path.join( log_dir, 'stoq_%s.log' % time.strftime('%Y-%m-%d_%H-%M-%S')) from kiwi.log import set_log_file self._stream = set_log_file(self._log_filename, 'stoq*') if hasattr(os, 'symlink'): link_file = os.path.join(stoqdir, 'stoq.log') if os.path.exists(link_file): os.unlink(link_file) os.symlink(self._log_filename, link_file) # We want developers to see deprecation warnings. from stoqlib.lib.environment import is_developer_mode if is_developer_mode(): import warnings warnings.filterwarnings("default", category=DeprecationWarning, module="^(stoq|kiwi)")
def _create_eggs_cache(self): log.info("Creating cache for plugins eggs") # $HOME/.stoq/plugins default_store = get_default_store() path = os.path.join(get_application_dir(), 'plugins') if not os.path.exists(path): os.makedirs(path) existing_eggs = { unicode(os.path.basename(f)[:-4]): md5sum_for_filename(f) for f in glob.iglob(os.path.join(path, '*.egg')) } # Now extract all eggs from the database and put it where stoq know # how to load them for plugin_name, egg_md5sum in default_store.using(PluginEgg).find( (PluginEgg.plugin_name, PluginEgg.egg_md5sum)): # A little optimization to avoid loading the egg in memory if we # already have a valid version cached. if existing_eggs.get(plugin_name, u'') == egg_md5sum: log.info("Plugin %r egg md5sum matches. Skipping it..." % (plugin_name, )) continue log.info("Creating egg cache for plugin %r" % (plugin_name, )) egg_filename = '%s.egg' % (plugin_name, ) plugin_egg = default_store.find(PluginEgg, plugin_name=plugin_name).one() with open(os.path.join(path, egg_filename), 'wb') as f: f.write(plugin_egg.egg_content)
def _prepare_logfiles(self): from stoqlib.lib.osutils import get_application_dir stoqdir = get_application_dir("stoq") log_dir = os.path.join(stoqdir, 'logs', time.strftime('%Y'), time.strftime('%m')) if not os.path.exists(log_dir): os.makedirs(log_dir) self._log_filename = os.path.join(log_dir, 'stoq_%s.log' % time.strftime('%Y-%m-%d_%H-%M-%S')) from kiwi.log import set_log_file self._stream = set_log_file(self._log_filename, 'stoq*') if hasattr(os, 'symlink'): link_file = os.path.join(stoqdir, 'stoq.log') if os.path.exists(link_file): os.unlink(link_file) os.symlink(self._log_filename, link_file) # We want developers to see deprecation warnings. from stoqlib.lib.environment import is_developer_mode if is_developer_mode(): import warnings warnings.filterwarnings( "default", category=DeprecationWarning, module="^(stoq|kiwi)")
def _prepare_logfiles(self): from stoqlib.lib.osutils import get_application_dir stoqdir = get_application_dir("stoq") log_dir = os.path.join(stoqdir, 'logs', time.strftime('%Y'), time.strftime('%m')) if not os.path.exists(log_dir): os.makedirs(log_dir) filename = 'stoq_%s.%s.log' % (time.strftime('%Y-%m-%d_%H-%M-%S'), os.getpid()) self._log_filename = os.path.join(log_dir, filename) from kiwi.log import set_log_file self._stream = set_log_file(self._log_filename, 'stoq*') if platform.system() != 'Windows': link_file = os.path.join(stoqdir, 'stoq.log') if os.path.exists(link_file): os.unlink(link_file) os.symlink(self._log_filename, link_file) # We want developers to see deprecation warnings. from stoqlib.lib.environment import is_developer_mode if is_developer_mode(): import warnings if self._options.non_fatal_warnings: action = "default" else: action = "error" warnings.filterwarnings(action, category=DeprecationWarning, module="^(stoq|kiwi)")
def _prepare_logfiles(self): from stoqlib.lib.osutils import get_application_dir stoqdir = get_application_dir("stoq") log_dir = os.path.join(stoqdir, 'logs', time.strftime('%Y'), time.strftime('%m')) if not os.path.exists(log_dir): os.makedirs(log_dir) filename = 'stoq_%s.%s.log' % (time.strftime('%Y-%m-%d_%H-%M-%S'), os.getpid()) self._log_filename = os.path.join(log_dir, filename) from kiwi.log import set_log_file self._stream = set_log_file(self._log_filename, 'stoq*') if platform.system() != 'Windows': link_file = os.path.join(stoqdir, 'stoq.log') if os.path.exists(link_file): os.unlink(link_file) os.symlink(self._log_filename, link_file) # We want developers to see deprecation warnings. from stoqlib.lib.environment import is_developer_mode if is_developer_mode() and not self._options.quiet: import warnings if self._options.non_fatal_warnings: action = "default" else: action = "error" warnings.filterwarnings( action, category=DeprecationWarning, module="^(stoq|kiwi)")
def setup_logging(): ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter( '%(asctime)s [%(processName)s(%(process)s)]: %(levelname)s - %(message)s') ch.setFormatter(formatter) root = logging.getLogger() root.setLevel(logging.INFO) root.addHandler(ch) if platform.system() == 'Windows': # FIXME: We need some kind of log rotation here log_dir = os.path.join(get_application_dir(), 'stoqserver-logs') if not os.path.exists(log_dir): os.makedirs(log_dir) log_filename = os.path.join(log_dir, multiprocessing.current_process().name) stdout_file = open(log_filename + '-stdout', 'a') # On windows, since it is not supervisor that is handling the logs, # and some places/plugins will do logging by printing info to stdout # (e.g. conector), we need to log them somewhere sys.stdout = _Tee(sys.stdout, stdout_file) sys.stderr = _Tee(sys.stderr, stdout_file) hdlr = logging.FileHandler(log_filename) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) root.addHandler(hdlr)
def test_get_application_dir_snap(mock_mkdir, monkeypatch): monkeypatch.setenv('SNAP', '/snap/test/') monkeypatch.setenv('SNAP_COMMON', '/var/snap/test/common') appdir = osutils.get_application_dir() assert appdir == '/var/snap/test/common' mock_mkdir.assert_called_once_with(appdir)
def test_get_application_dir_darwin(mock_mkdir, monkeypatch): monkeypatch.setenv('HOME', 'test_home') monkeypatch.setattr(osutils, '_system', 'Darwin') appdir = osutils.get_application_dir('test') assert appdir == os.path.join('test_home', 'Library', 'Application Support', 'Stoq') mock_mkdir.assert_called_once_with(appdir)
def test_get_application_dir_windows(mock_mkdir, monkeypatch): monkeypatch.setenv('ALLUSERSPROFILE', 'test_path') monkeypatch.setattr(osutils, '_system', 'Windows') appdir = osutils.get_application_dir('test') assert appdir == os.path.join('test_path', 'test') mock_mkdir.assert_called_once_with(appdir)
def test_get_application_dir_linux(mock_mkdir, monkeypatch): monkeypatch.setenv('HOME', 'test_home') monkeypatch.setattr(osutils, '_system', 'Linux') appdir = osutils.get_application_dir('test') assert appdir == os.path.join('test_home', '.test') mock_mkdir.assert_called_once_with(appdir)
def _setup_cookiefile(self): log.debug('setting up cookie file') from kiwi.component import provide_utility from stoqlib.lib.cookie import Base64CookieFile from stoqlib.lib.interfaces import ICookieFile from stoqlib.lib.osutils import get_application_dir app_dir = get_application_dir() cookiefile = os.path.join(app_dir, "cookie") provide_utility(ICookieFile, Base64CookieFile(cookiefile))
def _get_port(self): appdir = get_application_dir() portfile = os.path.join(appdir, 'daemon', self._daemon_id, 'port') try: data = open(portfile).read() except IOError, e: if e.errno == errno.ENOENT: raise TryAgainError raise
def stop(self): if not self._process: return os.kill(self._process.pid, signal.SIGINT) appdir = get_application_dir() daemondir = os.path.join(appdir, 'daemon', self._daemon_id) try: shutil.rmtree(daemondir) except OSError: pass
def _get_port(self): appdir = get_application_dir() portfile = os.path.join(appdir, 'daemon', self._daemon_id, 'port') try: data = open(portfile).read() except IOError as e: if e.errno == errno.ENOENT: raise TryAgainError raise return int(data)
def _migrate_from_pickle(self): username = api.get_current_user(api.get_default_store()).username filename = os.path.join(get_application_dir(), 'columns-%s' % username, self._restore_name + '.pickle') log.info("Migrating columns from pickle: %s" % (filename, )) try: with open(filename) as fd: import cPickle return cPickle.load(fd) except Exception, e: log.info("Exception while migrating: %r" % (e, )) return {}
def _get_save_location(self): stoq_dir = get_application_dir() # Until we finish the stoqnfe app, we will only export the nfe, so it # can be imported by an external application. # nfe_dir = os.path.join(stoq_dir, 'generated_nfe') nfe_dir = os.path.join(stoq_dir, "exported_nfe", time.strftime("%Y"), time.strftime("%m"), time.strftime("%d")) if not os.path.isdir(nfe_dir): os.makedirs(nfe_dir) return nfe_dir
def _get_save_location(self): stoq_dir = get_application_dir() # Until we finish the stoqnfe app, we will only export the nfe, so it # can be imported by an external application. # nfe_dir = os.path.join(stoq_dir, 'generated_nfe') nfe_dir = os.path.join(stoq_dir, 'exported_nfe', time.strftime('%Y'), time.strftime('%m'), time.strftime('%d')) if not os.path.isdir(nfe_dir): os.makedirs(nfe_dir) return nfe_dir
def _load_settings(self): self.config_dir = get_application_dir('stoq') settings = Gtk.PrintSettings() filename = os.path.join(self.config_dir, self.print_settings_name) if os.path.exists(filename): settings.load_file(filename) self.set_print_settings(settings) default_page_setup = Gtk.PageSetup() default_page_setup.set_orientation(Gtk.PageOrientation.LANDSCAPE) filename = os.path.join(self.config_dir, self.page_setup_name) if os.path.exists(filename): default_page_setup.load_file(filename) self.set_default_page_setup(default_page_setup)
def _load_settings(self): self.config_dir = get_application_dir('stoq') settings = gtk.PrintSettings() filename = os.path.join(self.config_dir, self.print_settings_name) if os.path.exists(filename): settings.load_file(filename) self.set_print_settings(settings) default_page_setup = gtk.PageSetup() default_page_setup.set_orientation(gtk.PAGE_ORIENTATION_PORTRAIT) filename = os.path.join(self.config_dir, self.page_setup_name) if os.path.exists(filename): default_page_setup.load_file(filename) self.set_default_page_setup(default_page_setup)
def load_settings(self, settings): """ Load data from a DatabaseSettings object :param settings: the settings object """ self.set("General", "logfile", os.path.join(get_application_dir(StoqConfig.domain), "application.log")) self.set("Database", "rdbms", settings.rdbms) self.set("Database", "address", settings.address) self.set("Database", "port", str(settings.port)) self.set("Database", "dbname", settings.dbname) self.set("Database", "testdb", settings.dbname) self.set("Database", "dbusername", settings.username) if settings.password: self.store_password(settings.password) self._settings = settings
def load_settings(self, settings): """ Load data from a DatabaseSettings object :param settings: the settings object """ self.set('General', 'logfile', os.path.join(get_application_dir(StoqConfig.domain), 'application.log')) self.set('Database', 'rdbms', settings.rdbms) self.set('Database', 'address', settings.address) self.set('Database', 'port', str(settings.port)) self.set('Database', 'dbname', settings.dbname) self.set('Database', 'dbusername', settings.username) if settings.password: self.store_password(settings.password) self._settings = settings
def _read_plugin_descriptions(self): # Development plugins on the same checkout paths = [os.path.join(library.get_root(), 'plugins')] # Plugins on $HOME/.stoq/plugins paths.append(os.path.join(get_application_dir(), 'plugins')) if library.get_resource_exists('stoq', 'plugins'): paths.append(library.get_resource_filename('stoq', 'plugins')) paths.extend(list(self._get_external_plugins_paths())) for path in paths: for filename in glob.iglob(os.path.join(path, '*', '*.plugin')): self._register_plugin_description(filename) for filename in glob.iglob(os.path.join(path, '*.egg')): self._register_plugin_description(filename, is_egg=True)
def setup_logging(app_name='stoq-server', is_debug=None): log_format = '%(asctime)s %(name)s [%(processName)s(%(process)s)]: %(levelname)s - %(message)s' ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG if is_debug else logging.INFO) if RequestIDLogFilter: ch.addFilter(RequestIDLogFilter()) log_format = log_format.replace('processName', 'request_id') formatter = logging.Formatter(log_format) ch.setFormatter(formatter) root = logging.getLogger() root.setLevel(logging.DEBUG if is_debug else logging.INFO) root.addHandler(ch) handler = SysLogHandler(address='/dev/log') handler.setLevel(logging.DEBUG) handler.setFormatter( logging.Formatter(app_name + '[%(process)d]: %(processName)s - %(message)s')) root.addHandler(handler) if platform.system() == 'Windows': # FIXME: We need some kind of log rotation here log_dir = os.path.join(get_application_dir(), 'stoqserver-logs') if not os.path.exists(log_dir): os.makedirs(log_dir) log_filename = os.path.join(log_dir, multiprocessing.current_process().name) stdout_file = open(log_filename + '-stdout.txt', 'a') # On windows, since it is not supervisor that is handling the logs, # and some places/plugins will do logging by printing info to stdout # (e.g. conector), we need to log them somewhere sys.stdout = _Tee(sys.stdout, stdout_file) sys.stderr = _Tee(sys.stderr, stdout_file) hdlr = logging.FileHandler(log_filename + '.txt') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) root.addHandler(hdlr)
def setup_logging(appname): from stoqlib.lib.osutils import get_application_dir stoqdir = get_application_dir(appname) log_dir = os.path.join(stoqdir, 'logs', time.strftime('%Y'), time.strftime('%m')) if not os.path.exists(log_dir): os.makedirs(log_dir) from kiwi.log import set_log_file _log_filename = os.path.join(log_dir, 'stoq_%s.log' % time.strftime('%Y-%m-%d_%H-%M-%S')) _stream = set_log_file(_log_filename, 'stoq*') if hasattr(os, 'symlink'): link_file = os.path.join(stoqdir, 'stoq.log') if os.path.exists(link_file): os.unlink(link_file) os.symlink(_log_filename, link_file) return _log_filename, _stream
def setup_logging(app_name='stoq-server'): # Note that kiwi creates another StreamHandler. If there is any indirect import from kiwi.log, # some lines will be duplicated. ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter( '%(asctime)s %(name)s [%(processName)s(%(process)s)]: %(levelname)s - %(message)s' ) ch.setFormatter(formatter) root = logging.getLogger() root.setLevel(logging.INFO) root.addHandler(ch) handler = SysLogHandler(address='/dev/log') handler.setLevel(logging.DEBUG) handler.setFormatter( logging.Formatter(app_name + '[%(process)d]: %(processName)s - %(message)s')) root.addHandler(handler) if platform.system() == 'Windows': # FIXME: We need some kind of log rotation here log_dir = os.path.join(get_application_dir(), 'stoqserver-logs') if not os.path.exists(log_dir): os.makedirs(log_dir) log_filename = os.path.join(log_dir, multiprocessing.current_process().name) stdout_file = open(log_filename + '-stdout.txt', 'a') # On windows, since it is not supervisor that is handling the logs, # and some places/plugins will do logging by printing info to stdout # (e.g. conector), we need to log them somewhere sys.stdout = _Tee(sys.stdout, stdout_file) sys.stderr = _Tee(sys.stderr, stdout_file) hdlr = logging.FileHandler(log_filename + '.txt') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) root.addHandler(hdlr)
def setup_logging(app_name='stoq-server'): # Note that kiwi creates another StreamHandler. If there is any indirect import from kiwi.log, # some lines will be duplicated. ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter( '%(asctime)s %(name)s [%(processName)s(%(process)s)]: %(levelname)s - %(message)s') ch.setFormatter(formatter) root = logging.getLogger() root.setLevel(logging.INFO) root.addHandler(ch) handler = SysLogHandler(address='/dev/log') handler.setLevel(logging.DEBUG) handler.setFormatter( logging.Formatter(app_name + '[%(process)d]: %(processName)s - %(message)s')) root.addHandler(handler) if platform.system() == 'Windows': # FIXME: We need some kind of log rotation here log_dir = os.path.join(get_application_dir(), 'stoqserver-logs') if not os.path.exists(log_dir): os.makedirs(log_dir) log_filename = os.path.join(log_dir, multiprocessing.current_process().name) stdout_file = open(log_filename + '-stdout.txt', 'a') # On windows, since it is not supervisor that is handling the logs, # and some places/plugins will do logging by printing info to stdout # (e.g. conector), we need to log them somewhere sys.stdout = _Tee(sys.stdout, stdout_file) sys.stderr = _Tee(sys.stderr, stdout_file) hdlr = logging.FileHandler(log_filename + '.txt') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) root.addHandler(hdlr)
def get_config_directory(self): return os.path.join(get_application_dir(self.domain))
import contextlib import hashlib import os import platform import shutil import subprocess import tempfile from OpenSSL import crypto from stoqlib.domain.certificate import Certificate from stoqlib.lib.osutils import get_application_dir from stoqlib.lib.settings import get_settings from stoqlib.lib.xmlutils import get_signer _is_windows = platform.system() == 'Windows' certdb_path = os.path.join(get_application_dir(), 'certdb') pkcs12_cert_path = os.path.join(certdb_path, 'cert.pfx') pkcs11_lib_path = os.path.join(certdb_path, 'cert.so') cert_path = { Certificate.TYPE_PKCS11: pkcs11_lib_path, Certificate.TYPE_PKCS12: pkcs12_cert_path, } def check_certdb(): """Check if the certdb exists.""" if not os.path.isdir(certdb_path): return False # FIXME: There's no nss on Windows
# Foundation, Inc., or visit: http://www.gnu.org/. # # Author(s): Stoq Team <*****@*****.**> # import os from stoqlib.lib.osutils import get_application_dir _ = lambda s: s # # General # APP_DIR = get_application_dir() APP_EGGS_DIR = os.path.join(APP_DIR, 'eggs') APP_CONF_FILE = os.path.join(APP_DIR, 'stoq.conf') APP_BACKUP_DIR = os.path.join(APP_DIR, 'scripts') SERVER_NAME = _('Stoq Server') SERVER_EGGS = ['kiwi.egg', 'stoqdrivers.egg', 'stoq.egg'] # FIXME: Windows SERVER_EGGS = [] SERVER_EXECUTABLE_EGG = 'stoq.egg' SERVER_AVAHI_PORT = 6969 SERVER_XMLRPC_PORT = 6970 SERVER_FLASK_PORT = 6971 # # Avahi
## along with this program; if not, write to the Free Software ## Foundation, Inc., or visit: http://www.gnu.org/. ## ## Author(s): Stoq Team <*****@*****.**> ## import os from stoqlib.lib.osutils import get_application_dir _ = lambda s: s # # General # APP_DIR = get_application_dir() APP_EGGS_DIR = os.path.join(APP_DIR, 'eggs') APP_CONF_FILE = os.path.join(APP_DIR, 'stoq.conf') APP_BACKUP_DIR = os.path.join(APP_DIR, 'backup') SERVER_NAME = _('Stoq Server') SERVER_EGGS = ['kiwi.egg', 'stoqdrivers.egg', 'stoq.egg'] # FIXME: Windows SERVER_EGGS = [] SERVER_EXECUTABLE_EGG = 'stoq.egg' SERVER_AVAHI_PORT = 6969 SERVER_XMLRPC_PORT = 6970 # # Avahi #
def _write_daemon_pids(self): appdir = get_application_dir() daemondir = os.path.join(appdir, 'daemon', self._daemon_id) os.makedirs(daemondir) port = os.path.join(daemondir, 'port') open(port, 'w').write('%s\n' % (self._xmlrpc.port, ))
def test_get_application_dir_unknown_system(monkeypatch): monkeypatch.setattr(osutils, '_system', 'Unknown') with pytest.raises(SystemExit): osutils.get_application_dir('test')
def get_filename(self): config_dir = get_application_dir(self.domain) return os.path.join(config_dir, 'settings')