Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for overriding Created and Last-Modified dates on deploy #336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions artifactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ def sha256sum(filename):
return sha256.hexdigest()


def datetime_xheader(datetime):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function datetime_xheader doesn't work with headers, it's a serializer.
if isinstance(datetime, datetime.datetime): doesn't it lead to a bug? because datetime is not a module anymore, it's a function argument datetime_xheader(datetime)

I think if we return int, not str - it works in headers too.

Could you add a test on this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must confess that I've not checked it, you're right the naming clash will likely mess this up, do you think it's necessary to allow the string version? I only used a datetime conversion at my code.

This is the code I've done as a hack, in which I've tried and the header works in setting up the created/last-modified date on the server:

def datetime_header(datetime):
    return str(int(round(1000*datetime.timestamp())))

# Custom version to allow change of Created and Last-Modified
def deploy_file(target, file_name, created, last_modified, parameters):
    md5 = md5sum(file_name)
    sha1 = sha1sum(file_name)
    sha256 = sha256sum(file_name)
    target = target / pathlib.Path(file_name).name

    with open(file_name, "rb") as fobj:
        url = str(target)

        matrix_parameters = (
            f";{encode_matrix_parameters(parameters)}" if parameters else None
        )
        headers = {}

        headers["X-Checksum-Md5"] = md5
        headers["X-Checksum-Sha1"] = sha1
        headers["X-Checksum-Sha256"] = sha256
        headers["X-Artifactory-Created"] = datetime_header(created)
        headers["X-Artifactory-Last-Modified"] = datetime_header(last_modified)

        target._accessor.rest_put_stream(
            url,
            fobj,
            headers=headers,
            session=target.session,
            verify=target.verify,
            cert=target.cert,
            timeout=target.timeout,
            matrix_parameters=matrix_parameters,
        )

if isinstance(datetime, datetime.datetime):
return str(int(round(1000 * datetime.timestamp())))
return datetime


def chunks(data, size):
"""
Get chink for dict, copy as-is from https://stackoverflow.com/a/8290508/6753144
Expand Down Expand Up @@ -1109,6 +1115,8 @@ def deploy(
explode_archive_atomic=None,
checksum=None,
by_checksum=False,
date_created=None,
date_last_modified=None,
):
"""
Uploads a given file-like object
Expand All @@ -1126,6 +1134,8 @@ def deploy(
:param explode_archive_atomic: (bool) if True, archive will be exploded in an atomic operation upon deployment
:param checksum: sha1Value or sha256Value
:param by_checksum: (bool) if True, deploy artifact by checksum, default False
:param date_created: (optional) datetime or str (POSIX timestamp in milliseconds), overrides Created date
:param date_last_modified: (optional) datetime or str (POSIX timestamp in milliseconds), overrides Last Modified date
"""

if fobj and by_checksum:
Expand Down Expand Up @@ -1155,6 +1165,12 @@ def deploy(
headers["X-Checksum-Deploy"] = "true"
if checksum:
headers["X-Checksum"] = checksum
if date_created:
headers["X-Artifactory-Created"] = datetime_xheader(date_created)
if date_last_modified:
headers["X-Artifactory-Last-Modified"] = datetime_xheader(
date_last_modified
)

self.rest_put_stream(
url,
Expand Down Expand Up @@ -1897,6 +1913,8 @@ def deploy(
parameters={},
explode_archive=None,
explode_archive_atomic=None,
date_created=None,
date_last_modified=None,
):
"""
Upload the given file object to this path
Expand All @@ -1910,6 +1928,8 @@ def deploy(
parameters=parameters,
explode_archive=explode_archive,
explode_archive_atomic=explode_archive_atomic,
date_created=date_created,
date_last_modified=date_last_modified,
)

def deploy_file(
Expand All @@ -1921,6 +1941,8 @@ def deploy_file(
parameters={},
explode_archive=False,
explode_archive_atomic=False,
date_created=None,
date_last_modified=None,
):
"""
Upload the given file to this path
Expand All @@ -1943,6 +1965,8 @@ def deploy_file(
parameters=parameters,
explode_archive=explode_archive,
explode_archive_atomic=explode_archive_atomic,
date_created=date_created,
date_last_modified=date_last_modified,
)

def deploy_by_checksum(
Expand All @@ -1951,6 +1975,8 @@ def deploy_by_checksum(
sha256=None,
checksum=None,
parameters={},
date_created=None,
date_last_modified=None,
):
"""
Deploy an artifact to the specified destination by checking if the
Expand All @@ -1969,6 +1995,8 @@ def deploy_by_checksum(
checksum=checksum,
by_checksum=True,
parameters=parameters,
date_created=date_created,
date_last_modified=date_last_modified,
)

def deploy_deb(
Expand Down