Skip to content

Commit

Permalink
Add experimental new high-level API prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Aug 7, 2021
1 parent 3cd5a29 commit 1eafa00
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 77 deletions.
98 changes: 98 additions & 0 deletions examples/python/manual_createrepo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env python

import os
import sys
import shutil
import os.path
import createrepo_c as cr

def manual_method(path):
# Prepare repodata/ directory
repodata_path = os.path.join(path, "repodata")
if os.path.exists(repodata_path):
x = 0
while True:
new_repodata_path = "%s_%s" % (repodata_path, x)
if not os.path.exists(new_repodata_path):
shutil.move(repodata_path, new_repodata_path)
break
x += 1
os.mkdir(repodata_path)

# Prepare metadata files
repomd_path = os.path.join(repodata_path, "repomd.xml")
pri_xml_path = os.path.join(repodata_path, "primary.xml.gz")
fil_xml_path = os.path.join(repodata_path, "filelists.xml.gz")
oth_xml_path = os.path.join(repodata_path, "other.xml.gz")
pri_db_path = os.path.join(repodata_path, "primary.sqlite")
fil_db_path = os.path.join(repodata_path, "filelists.sqlite")
oth_db_path = os.path.join(repodata_path, "other.sqlite")

pri_xml = cr.PrimaryXmlFile(pri_xml_path)
fil_xml = cr.FilelistsXmlFile(fil_xml_path)
oth_xml = cr.OtherXmlFile(oth_xml_path)
pri_db = cr.PrimarySqlite(pri_db_path)
fil_db = cr.FilelistsSqlite(fil_db_path)
oth_db = cr.OtherSqlite(oth_db_path)

# List directory and prepare list of files to process
pkg_list = []
with os.scandir(path) as entries:
for entry in entries:
if entry.is_file() and entry.path.endswith(".rpm"):
pkg_list.append(entry.path)

pri_xml.set_num_of_pkgs(len(pkg_list))
fil_xml.set_num_of_pkgs(len(pkg_list))
oth_xml.set_num_of_pkgs(len(pkg_list))

# Process all packages
for filename in pkg_list:
pkg = cr.package_from_rpm(filename)
pkg.location_href = os.path.basename(filename)
print("Processing: %s" % pkg.nevra())
pri_xml.add_pkg(pkg)
fil_xml.add_pkg(pkg)
oth_xml.add_pkg(pkg)
pri_db.add_pkg(pkg)
fil_db.add_pkg(pkg)
oth_db.add_pkg(pkg)

pri_xml.close()
fil_xml.close()
oth_xml.close()

# Note: DBs are still open! We have to calculate checksums of xml files
# and insert them to the databases first!

# Prepare repomd.xml
repomd = cr.Repomd()

# Add records into the repomd.xml
repomdrecords = (("primary", pri_xml_path, pri_db),
("filelists", fil_xml_path, fil_db),
("other", oth_xml_path, oth_db),
("primary_db", pri_db_path, None),
("filelists_db", fil_db_path, None),
("other_db", oth_db_path, None))
for name, path, db_to_update in repomdrecords:
record = cr.RepomdRecord(name, path)
record.fill(cr.SHA256)
if (db_to_update):
db_to_update.dbinfo_update(record.checksum)
db_to_update.close()
repomd.set_record(record)

# Write repomd.xml
open(repomd_path, "w").write(repomd.xml_dump())

# DONE!

if __name__ == "__main__":
if len(sys.argv) != 2 or not os.path.isdir(sys.argv[1]):
print("Usage: %s <directory>" % (sys.argv[0]))
sys.exit(1)

manual_method(sys.argv[1])

print("Repository created in %s" % sys.argv[1])
90 changes: 16 additions & 74 deletions examples/python/simple_createrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,93 +6,35 @@
import os.path
import createrepo_c as cr

def do_repodata(path):
# Prepare repodata/ directory
repodata_path = os.path.join(path, "repodata")
if os.path.exists(repodata_path):
x = 0
while True:
new_repodata_path = "%s_%s" % (repodata_path, x)
if not os.path.exists(new_repodata_path):
shutil.move(repodata_path, new_repodata_path)
break
x += 1
os.mkdir(repodata_path)

# Prepare metadata files
repomd_path = os.path.join(repodata_path, "repomd.xml")
pri_xml_path = os.path.join(repodata_path, "primary.xml.gz")
fil_xml_path = os.path.join(repodata_path, "filelists.xml.gz")
oth_xml_path = os.path.join(repodata_path, "other.xml.gz")
pri_db_path = os.path.join(repodata_path, "primary.sqlite")
fil_db_path = os.path.join(repodata_path, "filelists.sqlite")
oth_db_path = os.path.join(repodata_path, "other.sqlite")

pri_xml = cr.PrimaryXmlFile(pri_xml_path)
fil_xml = cr.FilelistsXmlFile(fil_xml_path)
oth_xml = cr.OtherXmlFile(oth_xml_path)
pri_db = cr.PrimarySqlite(pri_db_path)
fil_db = cr.FilelistsSqlite(fil_db_path)
oth_db = cr.OtherSqlite(oth_db_path)

def create_repo(path):
# List directory and prepare list of files to process
pkg_list = []
for filename in os.listdir(path):
filename = os.path.join(path, filename)
if os.path.isfile(filename) and filename.endswith(".rpm"):
pkg_list.append(filename)

pri_xml.set_num_of_pkgs(len(pkg_list))
fil_xml.set_num_of_pkgs(len(pkg_list))
oth_xml.set_num_of_pkgs(len(pkg_list))
with os.scandir(path) as entries:
for entry in entries:
if entry.is_file() and entry.path.endswith(".rpm"):
pkg_list.append(entry.path)

writer = cr.RepositoryWriter(
path,
unique_filenames=True,
with_sqlite=True,
)
writer.set_num_of_pkgs(len(pkg_list))

# Process all packages
for filename in pkg_list:
pkg = cr.package_from_rpm(filename)
pkg.location_href = os.path.basename(filename)
pkg = writer.add_pkg_from_file(filename)
print("Processing: %s" % pkg.nevra())
pri_xml.add_pkg(pkg)
fil_xml.add_pkg(pkg)
oth_xml.add_pkg(pkg)
pri_db.add_pkg(pkg)
fil_db.add_pkg(pkg)
oth_db.add_pkg(pkg)

pri_xml.close()
fil_xml.close()
oth_xml.close()

# Note: DBs are still open! We have to calculate checksums of xml files
# and insert them to the databases first!

# Prepare repomd.xml
repomd = cr.Repomd()

# Add records into the repomd.xml
repomdrecords = (("primary", pri_xml_path, pri_db),
("filelists", fil_xml_path, fil_db),
("other", oth_xml_path, oth_db),
("primary_db", pri_db_path, None),
("filelists_db", fil_db_path, None),
("other_db", oth_db_path, None))
for name, path, db_to_update in repomdrecords:
record = cr.RepomdRecord(name, path)
record.fill(cr.SHA256)
if (db_to_update):
db_to_update.dbinfo_update(record.checksum)
db_to_update.close()
repomd.set_record(record)

# Write repomd.xml
open(repomd_path, "w").write(repomd.xml_dump())
writer.finish()

# DONE!
# DONE !!

if __name__ == "__main__":
if len(sys.argv) != 2 or not os.path.isdir(sys.argv[1]):
print("Usage: %s <directory>" % (sys.argv[0]))
sys.exit(1)

do_repodata(sys.argv[1])
create_repo(sys.argv[1])

print("Repository created in %s" % sys.argv[1])
4 changes: 2 additions & 2 deletions src/compression_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ typedef enum {
CR_CW_MODE_SENTINEL, /*!< Sentinel of the list */
} cr_OpenMode;

/** Stat build about open content during compression (writting).
/** Stat build about open content during compression (writing).
*/
typedef struct {
gint64 size; /*!< Size of content */
Expand Down Expand Up @@ -86,7 +86,7 @@ typedef struct {
void *INNERFILE; /*!< Pointer to underlying FILE */
cr_OpenMode mode; /*!< Mode */
cr_ContentStat *stat; /*!< Content stats */
cr_ChecksumCtx *checksum_ctx; /*!< Checksum contenxt */
cr_ChecksumCtx *checksum_ctx; /*!< Checksum context */
} CR_FILE;

#define CR_CW_ERR -1 /*!< Return value - Error */
Expand Down
2 changes: 1 addition & 1 deletion src/createrepo_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ main(int argc, char **argv)
fil_db_filename = g_strconcat(tmp_out_repo, "/filelists.sqlite", NULL);
oth_db_filename = g_strconcat(tmp_out_repo, "/other.sqlite", NULL);
} else {
g_debug("Creating databases localy");
g_debug("Creating databases locally");
const gchar *tmpdir = g_get_tmp_dir();
pri_db_filename = g_build_filename(tmpdir, "primary.XXXXXX.sqlite", NULL);
fil_db_filename = g_build_filename(tmpdir, "filelists.XXXXXX.sqlite", NULL);
Expand Down
Loading

0 comments on commit 1eafa00

Please sign in to comment.