Skip to content

Commit

Permalink
* **New:** Repository instances now have a code attribute whi…
Browse files Browse the repository at this point in the history
…ch is used for generating the environment variables where in previous versions the ``id`` attribute has been used which caused difficulties in transferring the data to a different installation of Stalker. Also to make the system backwards compatible, Stalker will still set the old ``id`` based environment variables. But when asked for an environment variable it will return the ``code`` based one. The ``code`` argument as usual has to be initialized on ``Repository`` instance creation. That's why this version is slightly backwards incompatible and needs the database to be updated with Alembic (with the command ``alembic update head``).
  • Loading branch information
eoyilmaz committed Jan 1, 2020
1 parent dc3248b commit 52c9335
Show file tree
Hide file tree
Showing 39 changed files with 367 additions and 86 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
Stalker Changes
===============

0.2.24
======

* **New:** ``Repository`` instances now have a ``code`` attribute which is
used for generating the environment variables where in previous versions the
``id`` attribute has been used which caused difficulties in transferring the
data to a different installation of Stalker. Also to make the system
backwards compatible, Stalker will still set the old ``id`` based environment
variables. But when asked for an environment variable it will return the
``code`` based one. The ``code`` argument as usual has to be initialized on
``Repository`` instance creation. That's why this version is slightly
backwards incompatible and needs the database to be updated with Alembic
(with the command ``alembic update head``).

0.2.23
======

Expand Down
2 changes: 1 addition & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ script_location = alembic
# revision_environment = false

#sqlalchemy.url = sqlite:///%(here)s/stalker.db
sqlalchemy.url = postgresql://stalker_admin:stalker@localhost:5432/stalker
sqlalchemy.url = postgresql://stalker_admin:stalker@localhost:5432/stalker_home


# Logging configuration
Expand Down
42 changes: 42 additions & 0 deletions alembic/versions/bf67e6a234b4_added_revision_code_attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Added Revision.code attribute
Revision ID: bf67e6a234b4
Revises: ed0167fff399
Create Date: 2020-01-01 09:50:19.086342
"""

# revision identifiers, used by Alembic.
revision = 'bf67e6a234b4'
down_revision = 'ed0167fff399'

from alembic import op
import sqlalchemy as sa

import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def upgrade():
# add the column
logger.info("creating code column in Repositories table")
op.add_column(
'Repositories',
sa.Column('code', sa.String(length=256), nullable=True)
)

# copy the name as code
logger.info("filling data to the code column in Repositories table from Repositories.name column")
op.execute("""UPDATE "Repositories"
SET code = (
SELECT REGEXP_REPLACE(name, '\s+', '') from "SimpleEntities" where id="Repositories".id
)
""")
logger.info("set code column to not nullable")
op.alter_column('Repositories', 'code', nullable=False)


def downgrade():
logger.info("removing code column from Repositories table")
op.drop_column('Repositories', 'code')
11 changes: 10 additions & 1 deletion docs/source/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,18 @@ workstations/render farmers::
# and the repository itself
commercial_repo = Repository(
name="Commercial Repository"
name="Commercial Repository",
code="CR"
)

.. versionadded:: 0.2.24

Starting with Stalker version 0.2.24 :class:`.Repository` instances have
:attr:`stalker.models.repository.Repository.code` attribute to help
generating universal paths (both across OSes and different installations of
Stalker).


:class:`.Repository` class will be explained in detail in upcoming sections.

So::
Expand Down
3 changes: 2 additions & 1 deletion docs/source/tutorial_files/tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@

# and the repository itself
commercial_repo = Repository(
name="Commercial Repository"
name="Commercial Repository",
code="CR"
)

new_project = Project(
Expand Down
3 changes: 2 additions & 1 deletion stalker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ class Config(ConfigBase):
# Storage for uploaded files
server_side_storage_path=os.path.expanduser('~/Stalker_Storage'),

repo_env_var_template='REPO%(id)s',
repo_env_var_template='REPO%(code)s',
repo_env_var_template_old='REPO%(id)s',

#
# Tells Stalker to create an admin by default
Expand Down
7 changes: 6 additions & 1 deletion stalker/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ def create_repo_vars():
from stalker import defaults, Repository
all_repos = Repository.query.all()
for repo in all_repos:
os.environ[defaults.repo_env_var_template % {'id': repo.id}] = \
os.environ[repo.env_var] = repo.path

# TODO: Remove this in upcoming versions.
# This is added for backwards compatibility
os.environ[
defaults.repo_env_var_template_old % {'id': repo.id}] = \
repo.path


Expand Down
72 changes: 60 additions & 12 deletions stalker/models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from sqlalchemy import event, Column, Integer, ForeignKey, String
from sqlalchemy.orm import validates
from stalker.models.entity import Entity
from stalker.models.mixins import CodeMixin

from stalker.log import logging_level
import logging
Expand All @@ -29,7 +30,7 @@
logger.setLevel(logging_level)


class Repository(Entity):
class Repository(Entity, CodeMixin):
"""Manages fileserver/repository related data.
A repository is a network share that all users have access to.
Expand All @@ -44,6 +45,27 @@ class Repository(Entity):
Setting a path that contains backward slashes ("\\"), will be converted to
a path with forward slashes.
.. versionadded:: 0.2.24
Code attribute
Starting with v0.2.24 Repository instances have a new :attr:`.code`
attribute whose value is used by the
:class:`stalker.models.studio.Studio` to generate environment variables
that contains the path of this
:class:`stalker.models.repository.Repository` (i.e.
$REPOCP/path/to/asset.ma ``CP`` here is the ``Repository.code``) so that
instead of using absolute full paths one can use the
:attr:`.make_relative`` path to generate a universal path that can be
used across OSes and different installations of Stalker.
:param code: The code of the :class:`stalker.models.repository.Repository`.
This attribute value is used by the :class:`stalker.models.studio.Studio`
to generate environment variables that contains the path of this
``Repository`` (i.e. $REPOCP/path/to/asset.ma) so that instead of using
absolute full paths one can use the ``repository_relative`` path to
generate a universal path that can be used across OSes and different
installations of Stalker.
:param linux_path: shows the linux path of the repository root, should be a
string
Expand Down Expand Up @@ -84,11 +106,14 @@ class Repository(Entity):
osx_path = Column(String(256)) # /

def __init__(self,
code="",
linux_path="",
windows_path="",
osx_path="",
**kwargs):
kwargs['code'] = code
super(Repository, self).__init__(**kwargs)
CodeMixin.__init__(self, **kwargs)

self.linux_path = linux_path
self.windows_path = windows_path
Expand All @@ -109,11 +134,19 @@ def _validate_linux_path(self, key, linux_path):

linux_path = linux_path.replace("\\", "/")

if self.code is not None and platform.system() == "Linux":
# update the environment variable
from stalker import defaults
os.environ[
defaults.repo_env_var_template % {'code': self.code}
] = linux_path

if self.id is not None and platform.system() == "Linux":
# update the environment variable
from stalker import defaults
os.environ[defaults.repo_env_var_template % {'id': self.id}] = \
linux_path
os.environ[
defaults.repo_env_var_template_old % {'id': self.id}
] = linux_path

return linux_path

Expand All @@ -132,14 +165,19 @@ def _validate_osx_path(self, key, osx_path):

osx_path = osx_path.replace("\\", "/")

if self.id is not None and platform.system() == "Darwin":
from stalker import defaults
if self.code is not None and platform.system() == "Darwin":
# update the environment variable
from stalker import defaults
os.environ[defaults.repo_env_var_template % {'id': self.id}] = \
osx_path
os.environ[
defaults.repo_env_var_template % {'code': self.code}
] = osx_path

return osx_path
if self.id is not None and platform.system() == "Darwin":
os.environ[
defaults.repo_env_var_template_old % {'id': self.id}
] = osx_path

return osx_path

@validates("windows_path")
def _validate_windows_path(self, key, windows_path):
Expand All @@ -158,11 +196,19 @@ def _validate_windows_path(self, key, windows_path):
if not windows_path.endswith('/'):
windows_path += '/'

if self.code is not None and platform.system() == "Windows":
# update the environment variable
from stalker import defaults
os.environ[
defaults.repo_env_var_template % {'code': self.code}
] = windows_path

if self.id is not None and platform.system() == "Windows":
# update the environment variable
from stalker import defaults
os.environ[defaults.repo_env_var_template % {'id': self.id}] = \
windows_path
os.environ[
defaults.repo_env_var_template_old % {'id': self.id}
] = windows_path

return windows_path

Expand Down Expand Up @@ -325,7 +371,7 @@ def env_var(self):
"""returns the env var of this repo
"""
from stalker import defaults
return defaults.repo_env_var_template % {'id': self.id}
return defaults.repo_env_var_template % {'code': self.code}

def __eq__(self, other):
"""the equality operator
Expand All @@ -348,4 +394,6 @@ def receive_after_insert(mapper, connection, repo):
"""
logger.debug('auto creating env var for Repository with id: %s' % repo.id)
from stalker import defaults
os.environ[defaults.repo_env_var_template % {'id': repo.id}] = repo.path
os.environ[defaults.repo_env_var_template % {'code': repo.id}] = repo.path
os.environ[defaults.repo_env_var_template_old %
{'id': repo.id}] = repo.path
1 change: 1 addition & 0 deletions tests/benchmarks/task_total_logged_seonds.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@

test_repository = Repository(
name="Test Repository",
code="TR",
type=test_repository_type,
linux_path='/mnt/T/',
windows_path='T:/',
Expand Down
Loading

0 comments on commit 52c9335

Please sign in to comment.